#思路:因为要保留8月没有练题的人,所以不能把表连接以后用日期去筛选,而是要先筛选后做表连接; #①选出复旦的人,形成tb1; #②选出8月做题的人,形成tb2; #③tb1左联tb2,分组计算即可。 select tb1.device_id, university, count(question_id), count(if(result='wrong',null,result)) from (select device_id, university from user_profile where university='复旦大学') tb1 left join (select device_id, question_id, result from question_practice_detail where date_format(date,'%Y%m')='202108') tb2 on tb1.device_id=tb2.device_id group by tb1.device_id, university