,
1.
这是一张班级的成绩表,想统计出每个成绩段的数据,mysql的sql语句该怎么写呢。
select '60分以下' type, count(*) from student s where s.score < 60
union all
select '60分以上70分以下' type,count(*) from student s where s.score < 70 and s.score > 60
union all
select '70分以上80分以下' type,count(*) from student s where s.score < 80 and s.score > 70
union all
select '80分以上90分以下' type,count(*) from student s where s.score < 90 and s.score > 80
union all
select '90分以上' type,count(*) from student s where s.score > 90;
结果
+------------------+----------+
| type | count(*) |
+------------------+----------+
| 60分以下 | 1 |
| 60分以上70分以下 | 1 |
| 70分以上80分以下 | 2 |
| 80分以上90分以下 | 1 |
| 90分以上 | 3 |
+------------------+----------+
,用这样SELECT
sum(CASE when score sum(CASE when score>=60 and score sum(CASE when score>=70 and score sum(CASE when score>=80 and score sum(CASE when score>=90 and score<=100 then 1 else 0 end) AS '90分以上'
from student写出来的大家就不要黏贴了,我就算看到这个格式才会想出要怎么写出竖型的统计,我要的是我问的那个格式的。为方便大家,我创建表和插入数据如下CREATE TABLE student
(name
varchar(20) DEFAULT NULL,score
varchar(5) CHARACTER SET utf8 DEFAULT NULL
)
INSERT INTO student
VALUES ('张三', '82');
INSERT INTO student
VALUES ('李四', '80');
INSERT INTO student
VALUES ('王五', '95');
INSERT INTO student
VALUES ('赵六', '65');
INSERT INTO student
VALUES ('孙七', '95');
在此先谢谢各位前来帮忙的朋友