有表X(a1,a2)
a1     a2
----------
01     a
01     b
02     a
02     b
03     a有表Y(a1,a2)
a1     a2
----------
01     a
01     b
02     a
02     b
03     a
03     b请问如何通过sql来得到如下结果集?谢谢!
a1     a2
----------
03     b

解决方案 »

  1.   

    select * from Y where not exists(select 1 from X where a1=Y.a1 and a2=Y.a2)
      

  2.   

    to libin_ftsafe 
    你的sql没有用啊!
      

  3.   

    declare @a table (a1 varchar(10),a2 varchar(10))
    insert into @a select '01','a'
    insert into @a select '01','b'
    insert into @a select '02','a'
    insert into @a select '02','b'
    insert into @a select '03','a'
    declare @b table (a1 varchar(10),a2 varchar(10))
    insert into @b select '01','a'
    insert into @b select '01','b'
    insert into @b select '02','a'
    insert into @b select '02','b'
    insert into @b select '03','a'
    insert into @b select '03','b'select * from @b y where 
    not exists(select 1 from @a where a1=Y.a1 and a2=Y.a2)a1 a2
    03 b怎么会没有用啊!
      

  4.   


    select * from Y 
    where not exists(select 1 from X where X.a1=Y.a1 and X.a2=Y.a2)
      

  5.   

    SELECT a1, a2 FROM  b 
    EXCEPT
    SELECT a1, a2FROM a
      

  6.   

    select * from Y where not exists (select 1 from X where X.a1=Y.a1 and X.a2=Y.a2)
      

  7.   

    select * from Y 
    where not exists(select * from X,Y where X.a1=Y.a1 and X.a2=Y.a2)看到红色部分没有?,要联表查询~~~~