这是我的sql语句:select user.id,user
.mobile, user
.name, sum(actionType=1) 键盘,sum(actionType in(2,3,4)) app,sum(actionType in(1,2,3,4)) 合计 from user LEFT JOIN userecord on user
.id=userecord.userId where userecord.actionTime between '2016-12-01 00:00:00' and '2016-12-01 23:59:59' and userecord.userId between 226 and 244 group by user
.id; 我的原意是统计出266到244之间所有用户的操作记录,但是有些用户在这个时间段没有操作,所以查询结果就没有显示出来,下面是结果。
不知道你所谓的空数据是啥东西,你还是仔细查查你的where条件是不是不完善
你是啥数据库 如果是oracle 你可以利用decode(user.name,'',0,user.name) 每个字段都这样查 表示如果user.name为空 则显示0 如果不为空则显示user.name的值
糟糕的提问代码。。。。。。。。。。。
把时间过滤条件改做连接条件,userId过滤条件改用user表字段。
select user.id,user.mobile,
user.name,
sum(actionType=1) 键盘,
sum(actionType in(2,3,4)) app,
sum(actionType in(1,2,3,4)) 合计
from user
LEFT JOIN userecord
on user.id=userecord.userId
and userecord.actionTime between '2016-12-01 00:00:00' and '2016-12-01 23:59:59'
where user.userId between 226 and 244
group by user.id;
你就查找两个表的所有id相同的数据不可以嘛?
你的join条件有点问题
select user.id,user.mobile, user.name, sum(actionType=1) 键盘,sum(actionType in(2,3,4)) app,sum(actionType in(1,2,3,4)) 合计 from user LEFT JOIN userecord on user.id=userecord.userId and userecord.actionTime between '2016-12-01 00:00:00' and '2016-12-01 23:59:59' where userecord.userId between 226 and 244 group by user.id;