题解 | #统计复旦用户8月练题情况#

统计复旦用户8月练题情况

https://www.nowcoder.com/practice/53235096538a456b9220fce120c062b3

这题花时间在了两个知识点上如下:
1、直接用了count(result='right')来计数正确回答个数  这是错误的  应该用sum(if(result='right',1,0)
2、month(date)条件限制我放在了where后面,导致筛选出来的数据少了8月份没有做题的学生数据,
month(date)应该放在 left join on后面  
在使用left join时,on 和 where 条件的区别如下:
on条件是在生成临时表时使用的条件,它不管on中的条件是否为真,都会返回左边表中的记录。
where条件是在临时表生成好后,再对临时表进行过滤的条件。这时已经没有left join的含义(必须返回左边表的记录)了,条件不为真的就全部过滤掉。
select u.device_id,university,count(question_id) question_cnt,sum(if(result='right',1,0)) right_question_cnt from user_profile u  left join  question_practice_detail q on u.device_id=q.device_id and MONTH(q.date)=8
where university='复旦大学' 
group by u.device_id

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务