班级表class
成绩表source
学生表
1、查询班级一班的各科成绩最高的学生信息
使用左连接 left join
SELECT a.stuid, a.stuname, a.course, a.source, a.classname FROM ( SELECT student.stuid, student.stuname, source.course, source.source, class.classname FROM student LEFT JOIN class ON class.classid = student.classid LEFT JOIN source ON student.stuid = source.stuid WHERE class.classname = ‘一班‘ ) AS a LEFT JOIN ( SELECT student.stuid, student.stuname, source.course, source.source, class.classname FROM student LEFT JOIN class ON class.classid = student.classid LEFT JOIN source ON student.stuid = source.stuid WHERE class.classname = ‘一班‘ ) AS b ON a.source < b.source AND a.course = b.course WHERE b.course IS NULL