三重联接SQL

时间:2018-07-30 22:13:28

标签: sql sql-server join

如何在SQL中获得以下维恩图的红色部分 Venn Diag

感谢您的帮助!

4 个答案:

答案 0 :(得分:3)

我会首先想到except

select c.*
from c
except
select a.*
from a
except 
select b.*
from b;

我的下一个想法是not exists

select c.*
from t
where not exists (select 1 from a where a.id = c.id) and
      not exists (select 1 from b where b.id = c.id);

答案 1 :(得分:0)

使用Joins,您可以获取Ven图的任何部分,具体情况如下所示。

select c.* 
from TableC c
left join TableA a on a.id = c.id 
left join TableB b on b.id = c.id
Where a.id is null and b.id is null -- Records which does not match in both tables

答案 2 :(得分:0)

考虑到您没有给出实际的表结构,而是类似这样的事情,将会有些麻烦:

Select c.name
From c
Where not exists (select 1 from b where b.id = c.id)
  And not exists (select 1 from a where a.id = c.id);

答案 3 :(得分:0)

您可以使用NOT IN(假设id是常用属性):

SELECT c.id FROM c 
WHERE c.id NOT IN (SELECT a.id FROM a)
AND c.id NOT IN (SELECT b.id FROM b);