有两个完全相同的表T1和T2,有2个字段A和B,现要查出在T1.A=T2.A的情况下,T1.B不存在于T2.B的情况
T1                    T2
A     B                A      B
1     a                1      a
1     b                1      b
1     c                1      d
2     a                2      a
2     c                2      b
2     e                2      c查询结果:
A     B
1     c
2     e

解决方案 »

  1.   

    t1.a 不在 t2.a 要怎么办?
    select * from t1
    where not exists(
    select * from t2 where t1.a = t2.a and t1.b= t2.b)
      

  2.   

    select * from t1
    where exists(
    select * from t2 where t1.a = t2.a )
    and not exists(
    select * from t2 where t1.a = t2.a and t1.b= t2.b)
      

  3.   

    select t1.a,t1.b from t1 left join t2 on t1.B = t2.B and t2.a=t1.a where t2.a is null
      

  4.   

    遇到not exists 问题,可以逆向求相等,然后用外联接,求另一方为空的就是要的集合~性能大多数时候比not in和exists好些~书上是这么说的
      

  5.   

    最简洁的集合运算:
    select * from t1 minus select * from t2;