我有两个表aaa,bbb
aaa中有一个字段“Namea”
bbb中有一个字段“Nameb”
请问我如何得到表aaa中有,但是bbb中没有的记录
这个SQL语句怎么写??例如:
Namea:
x
y
zNameb:
x
m
y
返回的结果集应该是:
x
y希望大家帮忙!!!

解决方案 »

  1.   

    /* 试一下,还不知道你用的是什么数据库 */
    select Namea
    from aaa
    where (select count(*)
      from bbb
      where Nameb=Namea) > 0
      

  2.   

    select * from aaa where Namea not in (select Nameb from bbb)
      

  3.   

    select * from aaa where (not (aaa.namea in (select nameb from bbb)));
      

  4.   

    select Namea
    from aaa
    where (select count(*)
      from bbb
      where Nameb=Namea) = 0//或者select Namea
    from aaa
    where not exists(
      select Nameb
      from bbb
      where Nameb=Namea)
      

  5.   

    select a.namea from aaa a,bbb b where a.namea not in (select b.nameb from bbb b);
    在Oracle下调试通过。