题解 | #统计复旦用户8月练题情况#
https://www.nowcoder.com/practice/53235096538a456b9220fce120c062b3
SELECT up.device_id, up.university, IF(cid IS NULL,0,cid), IF(cr IS NULL,0,cr) AS question_cnt FROM user_profile up LEFT JOIN (SELECT q.device_id, COUNT(q.id) AS cid, SUM(IF(q.result='right',1,0)) AS cr FROM question_practice_detail q WHERE MONTH(q.date) = 8 GROUP BY q.device_id) AS s ON up.device_id = s.device_id WHERE up.university = '复旦大学';
1、先查出每个设备的答题情况,
SELECT q.device_id,COUNT(q.id),SUM(IF(q.result='right',1,0)) FROM question_practice_detail q WHERE MONTH(q.date) = 8 GROUP BY q.device_id;2、查出设备的归属学校情况,
SELECT up.device_id,up.university FROM user_profile up WHERE up.university='复旦大学';3、通过device_id归并两者的集合,并处理空值即可得到结果。