From d44aeed22022c0db1b67eda67ebfafb902775412 Mon Sep 17 00:00:00 2001 From: smallkun Date: Tue, 25 Feb 2025 09:51:43 +0800 Subject: [PATCH] Auto commit --- 2207/MySQL练习题/查询练习.md | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/2207/MySQL练习题/查询练习.md b/2207/MySQL练习题/查询练习.md index e66f589..94d10d8 100644 --- a/2207/MySQL练习题/查询练习.md +++ b/2207/MySQL练习题/查询练习.md @@ -332,9 +332,33 @@ FROM scores sc, students st WHERE sc.course_id = '01' AND sc.score < 60 AND sc.student_id = st.id ORDER BY sc.score DESC; -#13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 +#13.按平均成绩从高到低显示所有学生的所有课程的平均成绩 +SELECT c.`name`, AVG(sc.score) +FROM scores sc, courses c +WHERE sc.course_id = c.id +GROUP BY c.id +ORDER BY AVG(sc.score) DESC; #14.查询各科成绩最高分、最低分和平均分:以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率;(及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90) +SELECT c.id '课程ID', c.`name` '课程名称', MAX(s.score) '最高分', MIN(s.score) '最低分', ROUND(AVG(s.score), 1) '平均值' +FROM scores s, courses c +WHERE s.course_id = c.id +GROUP BY c.id; + +SELECT s.course_id, +CONCAT(TRUNCATE(COUNT(IF(s.score >=60, TRUE, NULL))/sc.count*100,2), '%') s1, +CONCAT(TRUNCATE(COUNT(IF(s.score >=70 AND s.score < 80, TRUE, NULL))/sc.count*100,2), '%') s2, +CONCAT(TRUNCATE(COUNT(IF(s.score >=80 AND s.score < 90, TRUE, NULL))/sc.count*100,2), '%') s3, +CONCAT(TRUNCATE(COUNT(IF(s.score >=90, TRUE, NULL))/sc.count*100,2), '%') s4 +FROM scores s, ( + SELECT course_id, COUNT(*) count + FROM scores + GROUP BY course_id +) sc +WHERE s.course_id = sc.course_id +GROUP BY s.course_id; + + #15.按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺