diff --git a/2207/MySQL练习题/查询练习.md b/2207/MySQL练习题/查询练习.md index 0fe2948..0719e0c 100644 --- a/2207/MySQL练习题/查询练习.md +++ b/2207/MySQL练习题/查询练习.md @@ -366,11 +366,45 @@ GROUP BY c.id; #16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 -#16.1查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 +#16.1查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 +SELECT sc.`name`, sc.s, @rank:=@rank+1 +FROM ( + SELECT st.`name`, IFNULL(SUM(sc.score),0) s + FROM students st + LEFT JOIN scores sc ON st.id = sc.student_id + GROUP BY st.id + ORDER BY s DESC +)sc, (SELECT @rank:=0) r; #17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 #18.查询各科成绩前三名的记录 +SELECT s1.sname, s1.cname, s1.score, s1.rank1 +FROM ( + SELECT st.`name` sname, c.`name` cname, sc.score, @rank1:=@rank1+1 rank1 + FROM students st, scores sc, courses c, (SELECT @rank1:=0) r + WHERE st.id = sc.student_id AND c.id = sc.course_id AND course_id = '01' + ORDER BY sc.score DESC + LIMIT 3 +)s1 +UNION +SELECT s2.sname, s2.cname, s2.score, s2.rank1 +FROM ( + SELECT st.`name` sname, c.`name` cname, sc.score, @rank2:=@rank2+1 rank1 + FROM students st, scores sc, courses c, (SELECT @rank2:=0) r + WHERE st.id = sc.student_id AND c.id = sc.course_id AND course_id = '02' + ORDER BY sc.score DESC + LIMIT 3 +)s2 +UNION +SELECT s3.sname, s3.cname, s3.score, s3.rank1 +FROM ( + SELECT st.`name` sname, c.`name` cname, sc.score, @rank3:=@rank3+1 rank1 + FROM students st, scores sc, courses c, (SELECT @rank3:=0) r + WHERE st.id = sc.student_id AND c.id = sc.course_id AND course_id = '03' + ORDER BY sc.score DESC + LIMIT 3 +)s3; #19.查询每门课程被选修的学生数