学生表A ID name
       1  AA
       2  BB
课程表B  ID cname  soccer
        1  语文    80
        1  数学    90
        2  语文    89 
        2  英语    70查询 即选择了 语文又选择了数学的学生信息,我这样写的
 
 select * from A
where id in(select t1.id from (select * from B
where cname='语文') t1,(select * from B
where cname='数学')t2where t1.id=t2.id)
 不知道有没有简单的方法???

解决方案 »

  1.   

    select * 
    from A
    where (select count(distinct cname) from B where id=a.id and cname in('语文','数学'))=2
      

  2.   

    ---测试数据---
    if object_id('[A]') is not null drop table [A]
    go
    create table [A]([ID] int,[name] varchar(2))
    insert [A]
    select 1,'AA' union all
    select 2,'BB'
    if object_id('[B]') is not null drop table [B]
    go
    create table [B]([ID] int,[cname] varchar(4),[soccer] int)
    insert [B]
    select 1,'语文',80 union all
    select 1,'数学',90 union all
    select 2,'语文',89 union all
    select 2,'英语',70
     
    ---查询---
    select * 
    from A
    where (select count(distinct cname) from B where id=a.id and cname in('语文','数学'))=2---结果---
    ID          name 
    ----------- ---- 
    1           AA(所影响的行数为 1 行)
      

  3.   


    select * from A where ID in

    select id from B where cname='数学' and 
    id in(select id from B where cname='语文')

    没有想到什么好方法,友情up
      

  4.   

    --用的2楼的数据.create table [A]([ID] int,[name] varchar(2))
    insert [A]
    select 1,'AA' union all
    select 2,'BB'
    create table [B]([ID] int,[cname] varchar(4),[soccer] int)
    insert [B]
    select 1,'语文',80 union all
    select 1,'数学',90 union all
    select 2,'语文',89 union all
    select 2,'英语',70select id from
    (
    select distinct id from b where cname = '语文'
    union all
    select distinct id from b where cname = '数学'
    ) t
    group by id having count(1) = 2
    /*
    id          
    ----------- 
    1(所影响的行数为 1 行)
    */
    select a.* from a where id in 
    (
    select id from
    (
    select distinct id from b where cname = '语文'
    union all
    select distinct id from b where cname = '数学'
    ) t
    group by id having count(1) = 2
    )
    /*ID          name 
    ----------- ---- 
    1           AA(所影响的行数为 1 行)*/drop table a , b
      

  5.   


    select * from A where id in
    (
    select id from B b1  where cname='语文' and exists (select * from B where id=b1.id and cname='数学')
    )select A.id,a.name from A join (select id from B b1  where cname='语文' and exists (select * from B where id=b1.id and cname='数学')) temp1 on a.id=temp1.id
      

  6.   

    declare @t1 table(sid int, sname varchar(10))
    insert @t1 select 1,'AA'
    union all select 2,'BB'
    declare @t2 table(cid int, cname nvarchar(8), cstore int)
    insert @t2 select 1, N'数学', 80
    union all select 1, N'语文', 90
    union all select 2, N'数学', 79
    union all select 2, N'英语', 80select 
        sname
    from 
        @t1 
    where not exists
        (select * from @t2 where cid=sid and cname not in (N'语文', N'数学'))
      

  7.   

    SELECT name FROM A INNER JOIN b bb ON a.id=bb.id JOIN b cc ON bb.id=cc.id WHERE bb.cname='语文' and cc.cname='数学'