求一条联合查询的sql解决方法
求一条联合查询的sql
以学生选课系统为例,有三个表
student,包括sid,sname;
course, 有cid,cname,关键是有一个是否停课的字段,假设为tingke
然后有一个关系表 selectcourse,字段是sid,cid以实现student和course的多对多连接
希望查询学生的课程里面没有停课的课程数目。大致结果应该这样
sid1 sname1 4
sid2 sname2 2
......
谢谢!
------解决方案--------------------
以学生选课系统为例,有三个表
student,包括sid,sname;
course, 有cid,cname,关键是有一个是否停课的字段,假设为tingke
然后有一个关系表 selectcourse,字段是sid,cid以实现student和course的多对多连接
希望查询学生的课程里面没有停课的课程数目。大致结果应该这样
sid1 sname1 4
sid2 sname2 2
......
谢谢!
------解决方案--------------------
- SQL code
select a.sid,a.sname, sum(case tingke when '没有停课' then 1 else 0 end) as 没有停课 from student a,course b,selectcourse c where a.sid=c.sid and b.cid=c.cid group by a.sid,a.sname