本文转自MYSQL|count()函数使用条件分别统计
在一条语句中统计出符合不同条件的行:
select class1_id
,class1_name
,class2_id
,class2_name
,run_id
,count(run_id) as count_all#提交数
,count(run_ok>0 or null) as count_done#处理数
,count(run_ok=1 or null) as count1#合格数
,count(run_ok=2 or null) as count2#不合格数
from `class2`
join `class1` on class2_class1_id=class1_id
left join `run` on run_class2_id=class2_id and date(run_addtime) between '$s_date1' and '$s_date2'
group by class2_id
order by class1_id,class2_id
输出结果: