表aid value
1   aa
2   bb表b
id value
1   aa
2   aaa
3   bbb要求取出在表a里有而表b里没有的value值。如例中应该选出bb,要求用Exists来写。

解决方案 »

  1.   

    select * from a where value not exists(select value from b)
      

  2.   

    where value not exists(select value from b)in 也可以实现where value not in (select value from b)
      

  3.   

    select * from a where not exists(select 1 from b where b.value =a.value )
      

  4.   

    上述各位说的方法只能满足单个字段匹配的情况;
    ——我的做法是:
    对于求存在于表a中、而不在表b中的数据,可以根据两个表的关键字(可多个)全匹配来查询;
      比如表A关键字为a1和b1,表B关键字为a2和b2,
      要求在表A中、不在表B中的记录,则可查询为:
      select A.a1, a.b1 from A
    where (a.a1+'/'+a.b1) not in (select (b.a2+'/'+b.b2) from B)
    ('/'可以省去,这里只是为了看上去直观而已;
      不过,前提条件是表A与表B的这2个字段数据类型是一致的,
      若不一致,要相应进行转换)