- 查詢“生物”課程比“物理”課程成績高的所有學生的學號;
阿新 • • 發佈:2018-06-02
case student color The lse bsp 查詢 name CA
-- 查詢“生物”課程比“物理”課程成績高的所有學生的學號; -- 方法一 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 fromscore 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
- 查詢“生物”課程比“物理”課程成績高的所有學生的學號;