name  course
a     语文
a     数学
a     化学
b     语文
b     英语
b     数据库
c     语文
c     数据库
c     物理求既选了数据库又选了物理的人结果
name  course
c     语文
c     数据库
c     物理

解决方案 »

  1.   

    select * from tb
    where name in ( select name from tb 
    where course in ('数据库','物理') 
    group by name 
    having count(1)=2
    )
      

  2.   

    SET NOCOUNT ON
    declare @t table(name varchar(10), course nvarchar(10)) 
    insert @t select 'a'    ,N'语文' 
    insert @t select 'a'    ,N'数学' 
    insert @t select 'a'    ,N'化学' 
    insert @t select 'b'    ,N'语文' 
    insert @t select 'b'    ,N'英语' 
    insert @t select 'b'   , N'数据库' 
    insert @t select 'c'  ,  N'语文' 
    insert @t select 'c' ,   N'数据库' 
    insert @t select 'c',    N'物理' 
    SELECT * FROM @T T WHERE (SELECT COUNT(*)FROM @T WHERE NAME=T.NAME AND COURSE IN(N'数据库',N'物理'))=2
    /*name       course
    ---------- ----------
    c          语文
    c          数据库
    c          物理*/
      

  3.   


    declare @table table (name varchar(1),course varchar(6))
    insert into @Table
    select 'a','语文' union all
    select 'a','数学' union all
    select 'a','化学' union all
    select 'b','语文' union all
    select 'b','英语' union all
    select 'b','数据库' union all
    select 'c','语文' union all
    select 'c','数据库' union all
    select 'c','物理'select c.* from @table c
    right join (
    select a.name from 
    (select * from @table where course='数据库') a join (select * from @table where course='物理') b
    on a.name=b.name) d
    on c.name=d.name/*
    name course
    ---- ------
    c    语文
    c    数据库
    c    物理
    */
      

  4.   

    select a.name from (select * from tb where tb.course="数据库") a ,(select * from tb where tb.course="物理" ) b where a.name=b.name
      

  5.   

    select a.* from tbl a ,tbl b
    where a.course = '数据库' and b.course = '物理' and a.name = b.name
      

  6.   

    create table #TT
    (
     [name] varchar(10),
     course varchar(20)
    )
    insert into #TT select 'a','语文'
    insert into #TT select 'a','数学'
    insert into #TT select 'a','化学'
    insert into #TT select 'b','语文'
    insert into #TT select 'b','英语'
    insert into #TT select 'b','数据库'
    insert into #TT select 'c','语文'
    insert into #TT select 'c','数据库'
    insert into #TT select 'c','物理'select T.* from #TT T join
    (
      select A.[name] from
    (
    select * from #TT where course='数据库'
    ) A join
    (
     select [name] from #TT where course='物理'
    ) B on A.[name]=B.[name]
    ) AB on T.[name]=AB.[name]name       course
    ---------- --------------------
    c          语文
    c          数据库
    c          物理(3 行受影响)