码迷,mamicode.com
首页 > 数据库 > 详细

mysql 分段统计数据

时间:2018-01-06 19:09:31      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:mysq   student   sum   字段   bsp   subject   问题   and   sel   

一个简单的分段统计的问题:student 表{id,name,score} 字段,统计各个分数段的人数。规则:60以下不及格,60-80良,80-100优。

SELECT
sum(CASE when score<60 then 1 else 0 end) AS ‘不及格‘,
sum(CASE when score>=60 and score<=80 then 1 else 0 end) AS ‘良‘,
sum(CASE when score>80 then 1 else 0 end) AS ‘优‘
FROM student;

 

SELECT 
(SELECT COUNT(1) FROM stuscore WHERE subject=a.subject AND score<60) 不及格,
(SELECT COUNT(1) FROM stuscore WHERE subject=a.subject AND score BETWEEN 60 AND 80) 良,
(SELECT COUNT(1) FROM stuscore WHERE subject=a.subject AND score>80) 优
FROM stuscore a GROUP BY a.subject;

 

mysql 分段统计数据

标签:mysq   student   sum   字段   bsp   subject   问题   and   sel   

原文地址:https://www.cnblogs.com/zzeng347/p/8214825.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!