题意: 统计复旦8月练题情况
- 注意点: 如果直接用left join question_practice_detail 后在where中进行过滤month(qpd.date)=8, 则学生信息中,只要8月份没有答题的也会被过滤掉
- 可以使用 left join question_practice_detail qpd on up.device_id=qpd.device_id and month(qpd.date)=8, 这样会先过滤b表8月份有答题的, 而不会把第一张表中8月份无答题的过滤掉
- case when 用法万能, 是我的最爱, 但是这个连接是全连接, 效率会低点
select up.device_id, university, sum(case when qpd.device_id is not null and month(qpd.date)=8 then 1 else 0 end) as question_cnt, sum(case when qpd.result = 'right' and month(qpd.date)=8 then 1 else 0 end) as right_question_cnt from user_profile up left join question_practice_detail qpd on up.device_id=qpd.device_id where up.university='复旦大学' group by up.device_id