A
id1 col1
1  aa
2  bb
3  ccB
id2 col2
1   aa
2   aa
3   bb现在要统计A的一个子集的记录数,这个子集满足的条件是col1在B中存在

解决方案 »

  1.   

    select a.* ,isnull(cnt,0) 子集数 from ta a
    left join (select col2,count(*) cnt from tb gruop by col2) b
      on a.col1=b.col2
      

  2.   

    select a.* from a where col1 in (select col2 from b)
      

  3.   


    select * from t1 a
    where exists(select 1 from t2 where id1=a.id1)
      

  4.   

    select   count(*) from   a   where   col1   in   (select   col2   from   b)
      

  5.   

    declare @a table(id1 int,col1 varchar(10))
    insert into @a select 1,'aa'
    union all
    select 2,'bb'
    union all
    select 3,'cc'declare @b table(id2 int,col2 varchar(10))
    insert into @b select 1,'aa'
    union all
    select 2,'aa'
    union all
    select 3,'bb'select count(*) 记录数 
    from @a a 
    where a.col1 in (select col2 from @b )