小弟这里有个问题,请达人们可以帮忙解答:
3张表A,B,C
A和B的字段完全相同,C表不同(但是其中2个字段a,b和A,B相同)
现在要查询A,B中的数据,条件是存在于A或者B中,但是a,b字段不存在于C中的请问这个SQL应该怎么写?小弟希望显示的字段不能重复急需,请达人们指教!
3张表A,B,C
A和B的字段完全相同,C表不同(但是其中2个字段a,b和A,B相同)
现在要查询A,B中的数据,条件是存在于A或者B中,但是a,b字段不存在于C中的请问这个SQL应该怎么写?小弟希望显示的字段不能重复急需,请达人们指教!
union
select a,b from B)
minus
select a,b from C;
union
select * from b where not exists ( select rowid from c where b.a = c.a and b.b = c.b)
;数据量不多用这个
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)
union
select a,b from B)
minus
select a,b from C;
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)
试试,满足你的要求?
这里用union all效率高一些