码迷,mamicode.com
首页 > 其他好文 > 详细

- 查询“生物”课程比“物理”课程成绩高的所有学生的学号;

时间:2018-06-02 17:42:41      阅读:541      评论:0      收藏:0      [点我收藏+]

标签:方法   成绩   --   pre   HERE   sele   style   case   学生   

-- 查询“生物”课程比“物理”课程成绩高的所有学生的学号;

-- 方法一

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

 

- 查询“生物”课程比“物理”课程成绩高的所有学生的学号;

标签:方法   成绩   --   pre   HERE   sele   style   case   学生   

原文地址:https://www.cnblogs.com/zjchao/p/9125937.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!