概述
mysql count函数同字段不同值计数
问题:
某个字段分组多种级别,要统计不同级别总数
解答如下:
使用sum if 等关键函数进行操作
select exam_id,subject_name,sum(if(grade = 1,1,0)) as '1级',sum(if(grade = 2,1,0)) as '2级',sum(if(grade = 3,1,0)) as '3级',sum(if(grade = 4,1,0)) as '4级',sum(if(grade = 5,1,0)) as '5级'
,sum(if(grade = 6,1,0)) as '6级',sum(if(grade = 7,1,0)) as '7级',sum(if(grade = 8,1,0)) as '8级',sum(if(grade = 9,1,0)) as '9级',sum(if(grade = 10,1,0)) as '10级'
from biz_exam_student
group by exam_id,subject_id order by exam_id ,subject_id;
缺点:
效率不好,还有待优化;
最后
以上就是高兴铅笔为你收集整理的mysql count函数同字段不同值计数的全部内容,希望文章能够帮你解决mysql count函数同字段不同值计数所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复