2.程序sql中bug优化

考察left join和where  ...  and  ...

# 查看数据是否补全,主要查看userid = 1356823用户
select tt1.userid,ifnull(tt3.totalSend,0) as totalSend,ifnull(tt1.tot,0) as total,ifnull(tt2.succ,0) as success,tt3.time as datetime  from (
 select userid,substring(createtime,1,10) as time,ifnull(count(1),0) as tot from gmi_message_user_his_new 
where substring(returntime,1,10)  = '2019-11-05' and status != '0' and userid = '1356823'
 group by userid,substring(createtime,1,10) 
 )tt1
left join
 (
 select userid,substring(createtime,1,10) as time,ifnull(count(1),0) as succ from gmi_message_user_his_new 
where substring(returntime,1,10)  = '2019-11-05' and status = 'DELIVRD' and status != '0' and userid = '1356823'
 group by userid,substring(createtime,1,10) 
 )tt2
 on tt1.userid = tt2.userid and tt1.time = tt2.time  
left join
 (
 select userid,substring(createtime,1,10) as time,ifnull(count(1),0) as totalSend from gmi_message_user_his_new 
where substring(createtime,1,10) >= '2019-11-01' and userid = '1356823'
 group by userid,substring(createtime,1,10) 
 )tt3
on tt1.userid = tt3.userid and tt1.time = tt3.time

 

A left join B与select * from A,B where .. and .. 

后者A,B中的数据,A如果包含的记录为3条,B只包含2条,最后where条件,得到的数据为2条。

前者中如果A表包含记录为3条,B只包含2条,之后得到3条,B中没有的数据为空。

 

如果A中统计的数据为B的总数,比如A为总数,B中为成功数,那么必须要包含所有的数据。必须通过left join去关联所有的数据。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值