表1
A1   A2
P1   T1
P2   T2表2 
A1   A2   A3
P1   T1   C1
P2   T2   C2
P1   T2   C3
P2   T1   D3只找出表2 中A1和A2列同时出现在表1中的列,
本例中希望的结果为:
A1   A2   A3
P1   T1   C1
P2   T2   C2

解决方案 »

  1.   

    select *
    from tb2 k
    where exists(select * from tb1 where k.a1=a1 and k.a2=a2 )
      

  2.   

    --> 测试数据:@table1
    declare @table1 table([A1] varchar(2),[A2] varchar(2))
    insert @table1
    select 'P1','T1' union all
    select 'P2','T2'
    declare @table2 table([A1] varchar(2),[A2] varchar(2),[A3] varchar(2))
    insert @table2
    select 'P1','T1','C1' union all
    select 'P2','T2','C2' union all
    select 'P1','T2','C3' union all
    select 'P2','T1','D3'select r.* from @table2 r join @table1 t
    on r.A1 = t.A1 and r.A2 = t.A2
    --------------------
    P1 T1 C1
    P2 T2 C2
      

  3.   

    select
     *
    from
     b t
    where
     exists(select * from a where a1=b.a1 and a2=b.a2)