select(sid,cid,grade) -> 选课表(学号,课程号,得分),求该表中选修了所有课程的学生学号?谢谢

解决方案 »

  1.   

    select sid,cid,grade 
    from  选课表
    where "选修了所有课程"的条件
      

  2.   

    如下表:
    sid     cid      grade
    001     c03      50
    001     c02      60
    002     c01      60
    003     c02      50
    002     c03      60
    001     c01      40
    只有001选了所有课,如何求
      

  3.   

    select sid from 选课表 
    group by sid
    having count(cid)=(select count(*) from 课程表)
      

  4.   

    这方法我知道,有没有不用count函数的方法?
      

  5.   

    select sid,cid,grade 
    from  选课表
    where cid in ('c01','c02','co3')
    group by sid
    ?????
      

  6.   

    select sid,cid,grade 
    from  选课表
    where cid in ('c01','c02','c03')
    group by sid这样可以吗?
      

  7.   

    ----创建测试数据
    declare @t table(sid varchar(10),cid varchar(10),grade int)
    insert @t
    select '001',     'c03',      50 union all
    select '001',     'c02',      60 union all
    select '002',     'c01',      60 union all
    select '003',     'c02',      50 union all
    select '002',     'c03',      60 union all
    select '001',     'c01',      40----查询
    select sid from @t group by sid 
    having count(*) = (select count(distinct cid) from @t)/*结果
    sid
    -----------------
    001
    */
      

  8.   

    flash 兄的明显不行,因为这里的c01等只是举个例子,不能固化,另外我只想求一个不用count函数的实现关系代数里除运算的例子,谢谢.
      

  9.   

    create table [select ]--选课表
    (
    sid int,
    cid int,
    grade int
    )insert into [select]
    select 1,1,80
    union  all
    select 1,2,70
    union all
    select 1,3,60
    union all
    select 2,1,70
    union all
    select 3,1,20
    union all
    select 3,2,80
    union all
    select 3,3,70
    create table [course]--课程名表
    (
    cid int,
    cname varchar(10)
    )
    insert into course
    select  1,'微机原理'
    union all
    select  2,'操作系统'
    union all
    select 3,'数据库'
    select sid from [select ]  group by sid  HAVING COUNT(*)>=(select count(*) from course)
      

  10.   

    再次声明,求不用count函数的sql
      

  11.   

    不用COUNT还有其他方法?除了数量想不出其他约束条件
      

  12.   

    create table #t(sid varchar(10),cid varchar(10),grade int)
    insert #t
    select '001',     'c03',      50 union all
    select '001',     'c02',      60 union all
    select '002',     'c01',      60 union all
    select '003',     'c02',      50 union all
    select '002',     'c03',      60 union all
    select '001',     'c01',      40 select DISTINCT sid from #t where sid not in(
    select  D.sid from (select * from (select sid
    from #t
    group by sid) A,(select DISTINCT cid from #t) B) as D
    where not exists(select 1 from #t where D.cid=cid and D.sid=sid))--如果存在课程表改为
    select DISTINCT sid from #t where sid not in(
    select  D.sid from (select * from (select sid
    from #t
    group by sid) A,课程表 B) as D
    where not exists(select 1 from #t where D.cid=cid and D.sid=sid))drop table #tsid        
    ---------- 
    001(所影响的行数为 1 行)表 '#t__________________________________________________________________________________________________________________00000000000F'。扫描计数 56,逻辑读 56 次,物理读 0 次,预读 0 次。我不知道having可不可以对集合进行操作,所以我就把学生的学号与课程表做迪卡尔集合运算,然后再求出他与原始表的差集,差集以外的学号就是要求的学号,不使用聚合函数。但是付出的代价就是性能,惨重的代价啊,多增加一行数据,扫描计数就上升14,我觉得要是可以还不如给表加约束,只求数量!本人不才,只能这样写了!
      

  13.   

    我还是喜欢这样写
    select sid from #t group by sid  HAVING COUNT(DISTINCT cid)=(select count(DISTINCT cid) from #t)
      

  14.   

    select sid from table a group by sid having count(*)=3
    感觉好像还是比较easy
      

  15.   

    为避免误操作时同一个人同一门课重复插入:select sid from tb 
    group by sid 
    having count(distinct cid) = 
    (select count(distinct cid) from tb)
      

  16.   

    这种问题一般都是两种方法
    count(distinct x)法或者 双重否定法 即 not exists(... where not in(..))
      

  17.   

    select(sid,cid,grade) -> 选课表(学号,课程号,得分),求该表中选修了所有课程的学生学号?谢谢==========
    select 学号
    from (select 学号,count(课程号) as 课程总数
          from 选课表
          group by 学号)
    where 课程总数=100门
      

  18.   

    declare @t table(sid varchar(10),cid varchar(10),grade int)
    insert @t
    select '001',     'c03',      50 union all
    select '001',     'c02',      60 union all
    select '002',     'c01',      60 union all
    select '003',     'c02',      50 union all
    select '002',     'c03',      60 union all
    select '001',     'c01',      40
    select distinct a.sid from @t a
    where not exists
    (select 1 from @t b
    where not exists(select 1 from @t where sid = a.sid and cid = b.cid))