select a.device_id, university, sum(if(month(date)=8,1,0)) as question_cnt, sum(if(result="right" and month(date)=8,1,0)) as right_question_cnt from user_profile a left join question_practice_detail b on a.device_id=b.device_id where university="复旦大学" group by a.device_id 首先由于需要保存用户没有答题得情况,须使用左链接保证user_profile 上的id 不会少 其次,month(date)=8 不能单纯写在where 里,哪怕加上 month(date) is not null --->会导致用户仅在非8月答题不在结果中显示(虽然这题并未设计该此数据) 解决方法一是直接写道两个sum函数里面通过if去计数,或者用left join 后接添加该条件的子链接