本帖最后由 gridrender 于 2012-12-17 13:21:26 编辑

解决方案 »

  1.   


    select distinct a.sno from 
    (
      select sno, cno, count(cno)over(partition by sno) as rn1 from sc
    ) a , 
    (
      select sno, cno, count(cno)over(partition by sno) as rn2 from sc where sno = '103'
    )b
    where a.cno = b.cno and a.rn1 = b.rn2 and a.sno != '103'测试通过。。
      

  2.   

    select sno from sc x where x.cno in (select cno from sc where sno='103' ) group by x.sno having count(x.cno)=(select count(cno) from sc where sno='103')   x.cno in (select cno from sc where sno='103' ) 这句话里肯定包含了103学生,所以查出来的数据包含103
      

  3.   

    select sno from sc x where x.cno in (select cno from sc where sno='103' ) group by x.sno having count(x.cno)=(select count(cno) from sc where sno='103')where x.cno in (select cno from sc where sno='103' )这个部分假设查出来是100,101
    由于你假了这个where限制,,所以select sno from sc x where x.cno in (select cno from sc where sno='103' ) 这个查出来永远是2条数据,刚好你的106也符合。然后再在这个查询结果基础上group by  。。count。怎么count(x.cno)都会是2条
      

  4.   

    #4已经解释的很清楚啦,因为你在where加了对课程的限制,所以对于106号学生来说,虽然他选了3门课,但是在这个where查询结果集里面只会返回两条,因为另一条课程记录是不满足你的where条件的。以此为基础的count当然是错误的。
    不信你试试新建一个学生,让这个学生选着4,5甚至更多的课程,只要包含103的那两门课程,那么用第一条sql也会将这位新学生错误的查询出来。
      

  5.   

    错误之后,我懂了,谢谢。
    按照我的写的语句,除了2#楼给的SQL查询语句之外,按照我写的SQL语句,应该如何修改才可以满足正确?
      

  6.   

    select sno from sc x where not exists (select 1 from sc y where y.sno = x.sno and y.cno not in (select cno from sc where sno='103' ))
    group by x.sno having count(x.cno)=(select count(cno) from sc where sno='103');这条应该满足你的要求,因为使用 not exists和not in能够保证选出来的sno,他选择的课程只会<= 103学生的课程数。,后面由于你使用了having count相等,所以最终选择出来的结果应该是你想要的。
      

  7.   

    select distinct sno from sc x where not exists
     ((select cno from sc y where y.sno=x.sno)
      contains 
      (select cno from sc where sno='108'))说明:SC是一个学生选课数据表,sno是学生学号,cno是课程编号,上述SQL语句的目标功能是“查询与108号学生选修的课程完全相同的其他学生的学号”
    (1)不管是哪种数据库,只要上述代码可以运行成功就好的,请给出相应的数据库名称及其版本号。
    (2)上面的contains表示集合包含运算符,如 A contains B 表示判断B集合是否是A集合的子集,类似于Oralce中的多集合比较运算符submultiset of的功能
    所以,上述代码中的contains不是表示数据库中的全文检索操作符,即首先创建全文索引,然后按照where contains(name,'张*')这样的语法来使用
    (3)上述代码我是从李建中编著的《数据库系统原理》(电子工业出版社)一书中看到的,语法上应该没有错,因为哪个教材上介绍了这种SQL操作符,但不知道为啥在实验中无法实现?
    (4)对于集合A和集合B,如果采用其他SQL查询语句实现判断A集合是B集合的完全子集?no exists的方法就算了,这个我已经有了,谢谢。