日期:2014-05-17  浏览次数:21377 次

怎么查询至少选修了 某个学生选修的所有课程
表结构如下:表名 SC
 
学号(sno)    选修的课程号(cno)
111              1
111              2
111              3
112              1
112              2


如何查询至少选修了 112学生选修的所有课程 的学生 ,真心求解
sql 数据库

------解决方案--------------------
哥们,你还是学生吧?是不是老师布置的作业呢?

select distinct sno from sc scx
 where not exists
  (select 1 from sc scy
    where scy.sno='112' and
          not exists
          (select 1 from sc scz
            where scz.sno=scx.sno and
                  scz.sno=scy.sno));
          

------解决方案--------------------
select t.sno
  from sc t
 where t.cno >= all (select m.cno from sc m where m.sno = '112')
   and t.sno <> '112'
一定是学生啦,大学的数据库书上就这么一个例子~~
------解决方案--------------------
select sno from sc where sc.cno in(select cno from sc where sno = '112');