比如 a表 有一个aId 字段, 此表有10行, aid分别为 1,2,3...10 
 b表也一样 有一个bId 字段, 此表有5行, aid分别为 1,2,3,4,5
该如何写一个查询语句,查询a表中不包含b表id的,好像 select a.* from a , b where a.aid!=b.bid 查到的数据不对啊,有人知道吗???

解决方案 »

  1.   

    我用 minus,你试试select * from a
    minus
    select * from a,b
    where a.aid=b.bid
      

  2.   

    select a.* 
    from a left outer join b
    on(a.aid=b.bid);
    外连接应该可以吧!
      

  3.   

    select * from a
    where id not in(select id from b);
      

  4.   

    楼主可以试一下以下的几个SQL语句:
    (1)用EXISTS语句:select aid from a where not exists (select 1 from b where a.aid=b.bid);(2)用IN 语句:select aid from a where aid not in(select bid from b);(3)用MINUS语句:select aid from a
    minus
    select bid from b;如果楼主的表中的数据量较大,建议使用第一种(EXISTS)方法.