复杂sql 分段写,最后合并

sub s 科目表 exam e 考试表 student st 学生表
id st_id id
sub_id
/* select MAX(e.score) from exam as e where e.subit = (select subid from sub where name='数学'); //数学成绩的最高分
select e.*,st.NAME st_name,sub.NAME sub_name,sub_teacher
from exam e
left join student st on e.stuid=st.stuid
left join sub on e.subid=sub.subid
where date='2016-06-20';//2016-06-20考试的所有学生,附带科目和教师。

select * from sub s ,exam e
left join student st on st.stuid=e.stuid where
e.score>=s.total0.6 and s.subid=e.subid and st.sex=1;//成绩超过及格线的男学生,输出姓名,考试分数/

/* $sql='
select a.class,a.jigeNum,b.bujigeNum
from (select class,count() as jigeNum from student where score>=60 group by class) as a,(select class,count() as bujigeNum from student where score<60 group by class) as b where a.class=b.class';*/
//查出每个班的及格人数和不及格人数,格式为 class 及格人数 不及格人数。
class 班级表 student 学生表 exam 分数表
id id uid
class_id grade

(select class_id,count() as jige from student where grade > 60 and s.id = e.uid group by class_id) as a
union
(select class_id,count() as bujige from student where grade < 60 and s.id = e.uid group by class_id) as b
作为新表
select a.class_id,a.jige,b.bujige from a ,b where a.class_id = b.class_id;

1 10
2 12
1 2
2 3 s 1 10 2 2 12 3

你可能感兴趣的:(复杂sql 分段写,最后合并)