同时选择语文和数学的学生的姓名,需要去重表:
姓名 课程
张三 语文
李四 数学
王二 语文
李四 语文
张三 数学
张三 英语选择结果:
张三
李四

解决方案 »

  1.   

    select 姓名
    from 表
    where 课程 in('语文','数学')
    group by 姓名
    having count(distinct 课程)=2
      

  2.   


    declare @t table (姓名 varchar(4),课程 varchar(4))
    insert into @t
    select '张三','语文' union all
    select '李四','数学' union all
    select '王二','语文' union all
    select '李四','语文' union all
    select '张三','数学' union all
    select '张三','英语'select 姓名 from
    (
    select * from @t where 课程='语文'
    union all
    select * from @t where 课程='数学'
    ) a group by 姓名 having(count(1)>1)
    /*
    姓名
    ----
    李四
    张三
    */
      

  3.   

    declare @tab table
    (
    Name nvarchar(50),
    KeC nvarchar(50)
    )insert into @tab(Name,Kec) 
    select '张三', '语文'
    union all
    select '李四', '数学'
    union all
    select '王二' ,'语文'
    union all
    select '李四', '语文'
    union all
    select '张三', '数学'
    union all
    select '张三' ,'英语'select * from @tab where Name in(select Name from @tab where Kec='语文')
    and keC='数学'
      

  4.   


    select distinct Name from @tab where Name in(select Name from @tab where Kec='语文')
    and keC='数学'
      

  5.   


    select distinct  Name from A where exists(select A where 课程 =语文)and 课程 =数学
      

  6.   

    Update!select distinct  Name from A where exists(select  0 from  A where 课程 =语文)and 课程 =数学
      

  7.   

    select distinct aa.姓名 from sb aa
    where exists
    (
    select 1 from sb bb 
    where aa.课程='语文' and bb.课程='数学' and aa.姓名=bb.姓名
    )
      

  8.   

    --同时选择语文和数学的学生的姓名,需要去重--表:
    --姓名 课程
    --张三 语文
    --李四 数学
    --王二 语文
    --李四 语文
    --张三 数学
    --张三 英语declare @t table (姓名 varchar(4),课程 varchar(4))
    insert into @t
    select '张三','语文' union all
    select '李四','数学' union all
    select '王二','语文' union all
    select '李四','语文' union all
    select '张三','数学' union all
    select '张三','英语'
    --选择结果:
    --张三
    --李四select  distinct 姓名 from @t where 课程 in('语文','数学')
     group by 姓名
    having count(distinct 课程)=2
    (6 行受影响)
    姓名
    ----
    李四
    张三(2 行受影响)
      

  9.   

    create table tb(姓名 varchar(4),课程 varchar(4))
    insert into tb
    select '张三','语文' union all
    select '李四','数学' union all
    select '王二','语文' union all
    select '李四','语文' union all
    select '张三','数学' union all
    select '张三','英语'
    go
    select a.姓名 from tb a left join tb b  on a.姓名=b.姓名 where a.课程='语文' and b.课程='数学'
    结果为:
    姓名
    张三
    李四
    (2 行受影响)