本帖最后由 bashen1101 于 2013-06-03 16:06:46 编辑

解决方案 »

  1.   


    SELECT Gp_id FROM b WHERE X IN (select X from A) GROUP BY Gp_id having count(1) =2
      

  2.   


    create table A(x int)insert into A
    select 1 union all
    select 2create table B(Gp_id int,x int)insert into B
    select 1,1 union all
    select 1,2 union all
    select 2,2 union all
    select 2,3 union all
    select 3,1 union all
    select 3,3 union all
    select 4,1 union all
    select 4,2
    select Gp_id
     from B
     inner join A on B.x=A.x
     group by Gp_id
     having count(1)=(select count(1) from A)/*
    Gp_id
    -----------
    1
    4(2 row(s) affected)
    */
    select Gp_id,x 
     from B 
     where Gp_id in
     (select Gp_id
      from B
      inner join A on B.x=A.x
      group by Gp_id
      having count(1)=(select count(1) from A)
     )/*
    Gp_id       x
    ----------- -----------
    1           1
    1           2
    4           1
    4           2(4 row(s) affected)
    */
      

  3.   


    select Gp_id from b inner join a on a.x = b.x group by Gp_id having count(1)=(select count(1) from a)
      

  4.   


    select * from b
    where GP_ID IN
    (select Gp_id from b inner join a on a.x = b.x 
    group by Gp_id having count(1)=(select count(1) from a))