-- 查询“生物”课程比“物理”课程成绩高的所有学生的学号;
-- 方法一
select * from score a LEFT JOIN course b on a.course_id = b.cid where b.cname='生物'
select * from score a LEFT JOIN course b on a.course_id = b.cid where b.cname='物理'
SELECT c.student_id,c.num,d.num from (select a.student_id,a.num,b.cname from score a LEFT JOIN course b on a.course_id = b.cid where b.cname='生物')as c LEFT JOIN
(select * from score a LEFT JOIN course b on a.course_id = b.cid where b.cname='物理' ) d on c.student_id = d.student_id WHERE c.num > IFNULL(d.num,0)
-- 方法2--
select a.student_id,MAX(case b.cname when '生物' THEN a.num ELSE 0 end) '生物' ,MAX(case b.cname when '物理' THEN a.num ELSE 0 end) '物理'
from score a LEFT JOIN course b on a.course_id = b.cid GROUP BY a.student_id HAVING 生物 > 物理
-- 方法3
select a.student_id, (select num from score h LEFT JOIN course m on h.course_id = m.cid where m.cname='生物' and h.student_id=a.student_id ) as shengwu,
(select num from score h LEFT JOIN course m on h.course_id = m.cid where m.cname='物理' and h.student_id=a.student_id ) as wuli from score a GROUP BY a.student_id