请各位大侠给小弟参考个sqla表和b表的表结构一样,a和b通过id关联,我如何取出a和b关联不上的数据,如果写出来,最好解释一下,谢谢!在线等

解决方案 »

  1.   


    select * from a
    where not exists(select id from b where a.id=b.id)--a中存在,b中不存在
    union all--合并两个结果,即为a,b关联不上的
    select * from b
    where not exists(select id from a where a.id=b.id);--b中存在,a中不存在
      

  2.   

    利用minus
    1.取得存在a但是不存在b的数据
    select * from a 
    minus 
    select * from b;2.取得存在b但是不存在a的数据
    select * from b
    minus
    select * from a;
      

  3.   

    不好意思 该是FULL OUTER JOIN
      

  4.   

    select * from (select a.id aid,b.id bid from a full join b on a.id=b.id) where 
    aid is null or bid is null
      

  5.   

    上面都有提到几种方法not exists、minus也可以反面思考  先获取a、b俩表有关联的id 然后再分别取出不等于有关联的id
      

  6.   

    很多方法~
    假设A表是主表,并且A.ID和B.ID都不为空,上面高手说过的不再重复.作用是从A表删除ID等于B.ID的记录:
    参考:SELECT
    A.COL1
    ,A.COL2
    ...
    ,A.COLN
    FROM TABLEA A, TABLEB B
    WHERE 1=1
    AND A.ID = B.ID(+)
    AND B.ID IS NULL
    ;
      

  7.   


    Sorry,理解漏了你的意思,上面的方式并不能将B中不存在于A的记录筛选出来.你只需要把顺序互换再union all就行.
      

  8.   

    你的意思就是把A表与B表id不相同的两表记录都拉出来吧,对于这个实现的方法有几种,比如not exists+union all,full outer join+where  is null:
    1.not exists+union all (注意union all两个数据集出来的列名(不相同可用别名)及类型要相同,这儿只输出id了)
      select id from a where  not exist(select 1 from b where b.id=a.id)  union all select id from b  where not exists(select 1 from a where a.id=b.id)
    2.full outer join+where  is null(即全表连接,取出关联不到其他表数据项即为需要的) select * from (select a.id as aid,b.* from a full outer join b on b.id=a.id where a.id is null or b.id is null)c