diff --git a/2207/MySQL练习题/查询练习.md b/2207/MySQL练习题/查询练习.md index 8ede1a9..8d9f6a2 100644 --- a/2207/MySQL练习题/查询练习.md +++ b/2207/MySQL练习题/查询练习.md @@ -177,4 +177,93 @@ insert into scores values('07', '03', 98); 44.查询本月过生日的学生 -45.查询下月过生日的学生 \ No newline at end of file +45.查询下月过生日的学生 + +```sql +#1.查询" 01 "课程比" 02 "课程成绩高的学生的信息及课程分数 +SELECT st.*, c1.score '01课程', c2.score '02课程' +FROM students st, ( + SELECT sc1.student_id, sc1.score + FROM scores sc1 + WHERE sc1.course_id = 1 +)c1, +( + SELECT sc2.student_id, sc2.score + FROM scores sc2 + WHERE sc2.course_id = 2 +)c2 +WHERE st.id = c1.student_id AND st.id = c2.student_id AND c1.score > c2.score; + +#1.1查询同时存在" 01 "课程和" 02 "课程的情况 +SELECT st.*, c1.score '01课程', c2.score '02课程' +FROM students st, ( + SELECT sc1.student_id, sc1.score + FROM scores sc1 + WHERE sc1.course_id = 1 +)c1, +( + SELECT sc2.student_id, sc2.score + FROM scores sc2 + WHERE sc2.course_id = 2 +)c2 +WHERE st.id = c1.student_id AND st.id = c2.student_id; + +#1.2查询存在" 01 "课程但可能不存在" 02 "课程的情况(不存在时显示为 null ) +SELECT st.*, c1.score '01课程', c2.score '02课程' +FROM students st +LEFT JOIN +( + SELECT sc1.student_id, sc1.score + FROM scores sc1 + WHERE sc1.course_id = 1 +)c1 ON st.id = c1.student_id +LEFT JOIN +( + SELECT sc2.student_id, sc2.score + FROM scores sc2 + WHERE sc2.course_id = 2 +)c2 ON st.id = c2.student_id +WHERE c1.score IS NOT NULL; + +#1.3查询不存在" 01 "课程但存在" 02 "课程的情况 +SELECT st.*, c1.score '01课程', c2.score '02课程' +FROM students st +LEFT JOIN +( + SELECT sc1.student_id, sc1.score + FROM scores sc1 + WHERE sc1.course_id = 1 +)c1 ON st.id = c1.student_id +LEFT JOIN +( + SELECT sc2.student_id, sc2.score + FROM scores sc2 + WHERE sc2.course_id = 2 +)c2 ON st.id = c2.student_id +WHERE c1.score IS NULL AND c2.score IS NOT NULL; + +#2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 +SELECT st.`name`, AVG(sc.score) +FROM students st, scores sc +WHERE st.id = sc.student_id +GROUP BY st.id +HAVING AVG(sc.score) > 60; + +#3.查询在 scores 表存在成绩的学生信息 +SELECT * +FROM students +WHERE students.id IN ( + SELECT student_id + FROM scores +); + +#4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +#4.1查有成绩的学生信息 + +#5.查询「李」姓老师的数量 + +#6.查询学过「张三」老师授课的同学的信息 + +#7.查询没有学全所有课程的同学的信息 +``` +