50个SQL语句(MySQL版) 问题二十三

--------------------------表结构--------------------------

student(StuId,StuName,StuAge,StuSex) 学生表

teacher(TId,Tname) 教师表

course(CId,Cname,C_TId) 课程表

sc(SId,S_CId,Score) 成绩表

----------------------------------------------------------

问题二十三:统计打印各科成绩各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[<60]

SELECT sc.S_CId "课程ID", course.Cname "课程名称",
SUM(CASE WHEN sc.Score BETWEEN 85 AND 100 THEN 1 ELSE 0 END) "[100-85]",
SUM(CASE WHEN sc.Score BETWEEN 70 AND 85 THEN 1 ELSE 0 END) "[85-70]",
SUM(CASE WHEN sc.Score BETWEEN 60 AND 70 THEN 1 ELSE 0 END) "[70-60]", 
SUM(CASE WHEN sc.Score<60 THEN 1 ELSE 0 END) "[<60]"
FROM sc,course
where sc.S_CId=course.CId
GROUP BY sc.S_CId;

答案仅供参考,不一定完全正确,若发现错误或有更好的,欢迎评论,互相交流,一起成长!!!

50个SQL语句(MySQL版) 问题二十三

上一篇:SpringBoot整合Flyway(数据库版本迁移工具)


下一篇:MySQL篇