小弟这里有个问题,请达人们可以帮忙解答:
3张表A,B,C
A和B的字段完全相同,C表不同(但是其中2个字段a,b和A,B相同)
现在要查询A,B中的数据,条件是存在于A或者B中,但是a,b字段不存在于C中的请问这个SQL应该怎么写?小弟希望显示的字段不能重复急需,请达人们指教!

解决方案 »

  1.   

    (select a,b from A
    union
    select a,b from B)
    minus
    select a,b from C;
      

  2.   

    select * from a where not exists ( select rowid from c where a.a = c.a and a.b = c.b)
    union
    select * from b where not exists ( select rowid from c where b.a = c.a and b.b = c.b)
    ;数据量不多用这个
      

  3.   

    select * from a
    where not exists(select 1 from c where a=a.a and b=a.b)
    union 
    select * from b
    where not exists(select 1 from c where a=b.a and b=b.b)
      

  4.   

    (select a,b from A 
    union 
    select a,b from B) 
    minus 
    select a,b from C; 
      

  5.   

    select a.* ,b.* 
    from a,b,c
    where 
    (select a.a ,a.b from a
    union all
    select b.a ,b.b from b
    ) not exist
    (select c.a ,c.b from c)
    试试,满足你的要求?
      

  6.   


    这里用union all效率高一些
      

  7.   

    #6还没想到更好的方式#7union all就有重复数据了。