題目34:成績有重復的情況下,查詢選修「張三」老師所授課程的學生中,成績最高的學生信息及其成績
分析:上一題 33 是在成績不重復的情況下 查詢選修「張三」老師所授課程的學生中,成績最高的學生信息及其成績
這兩題有什么區別? 有區別! 33 題成績不重復,得到的結果就是唯一的,可以直接limit 1 拿出來
但是:這一題目,成績有重復,再用limit 1 就會漏
思路:我們找到 選修「張三」老師所授課程的學生中,成績最高的分數 max(score) as a
然后再查詢一遍 分數 in(a) 就得到結果了
SELECT student.*,student_score.score FROM student_course, student_score, teacher,student
WHERE
teacher.id = student_course.teacherid
AND teacher.teacher_name = '張三'
AND student_course.id = student_score.courseid
AND student.id = student_score.studentid
AND student_score.score IN (
SELECT
MAX( score )
FROM
student_course,
student_score,
teacher,
student
WHERE
teacher.id = student_course.teacherid
AND teacher.teacher_name = '張三'
AND student_course.id = student_score.courseid
AND student.id = student_score.studentid
)

題目35:查詢不同課程成績相同的學生的學生編號、課程編號、學生成績
分析:這種情況可以通過兩張表join 查找
select a.courseid, a.studentid, a.score from student_score as a
inner join
student_score as b
on a.studentid = b.studentid
and a.courseid != b.courseid
and a.score = b.score;

