我是SQL的初学者。我有3个表(A,B和C),我想只提取属于B或C的A部分。
我知道A和C之间的交集是通过以下方式获得的:
A inner join C on #the keys
在A和B之间:
A inner join B on #the keys
我的问题是:如何加入/添加这两个内连接?
我会用exists
:
select . . .
from a
where exists (select 1 from b where b.? = a.?) or
exists (select 1 from c where b.? = a.?);
如果您想要所有表中的列,请使用left join
并使用where
子句:
select . . .
from a left join
b
on . . . left join
c
on . . .
where c.? is not null or b.? is not null;
我终于找到了如何做到这一点......它非常简单快捷:
SELECT ... FROM A
INNER JOIN B ON ....
UNION
SELECT ... FROM A
INNER JOIN C ON ....