题目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;