表Aid            Name
 
1            A
 
2            B
 
3            C
  表Bid            Name
 
2            E
 
3            F
 
4            G
  
根据ID判断
当表A中存在数据,表B不存在数据时,取表A的数据当表A中不存在数据,表B存在数据时,取表B的数据当表A中存在数据,表B存在数据时,取表B的数据 结果为id            Name
 
1            A
 
2            E
 
3            F
 
4            G
  一个SQL怎么写写啊,不要用取B表的所有数据,再UNION在A表中存在,B表中不存在的数据,这种方式。
解决方案就可以了

解决方案 »

  1.   

    就是说,表B中的优先select * from A where A.id not in
    (select distinct id from B)
    union
    select * from B
      

  2.   

    不要用取B表的所有数据,再UNION在A表中存在,B表中不存在的数据,这种方式。
    谢谢楼上的。
      

  3.   

    select nvl(b.id,a.id) id, nvl(b.name,a.name) name
    from b full outer join a
    on(a.id=b.id)
    order by id        ID N
    ---------- -
             1 A
             2 E
             3 F
             4 G