简体   繁体   English

SQL:内部连接的连接

[英]SQL : Join of inner join

I am a beginner on SQL.我是 SQL 的初学者。 I have 3 tables (A,B and C) and I want to extract only the part of A that is part of B or C.我有 3 个表(A、B 和 C),我只想提取属于 B 或 C 的 A 部分。

Here is an image of what I want to extract (in red):这是我要提取的图像(红色): 在此处输入图像描述

I know that the intersection between A and C is obtain by:我知道 A 和 C 之间的交集是通过以下方式获得的:

A inner join C on #the keys

and between A and B: A和B之间:

A inner join B on #the keys

My question is: how to join/add those two inner join?我的问题是:如何加入/添加这两个内部联接?

I would use exists :我会使用exists

select . . . 
from a
where exists (select 1 from b where b.? = a.?) or
      exists (select 1 from c where b.? = a.?);

If you want columns from all tables, then use left join and use a where clause:如果您想要所有表中的列,请使用left join并使用where子句:

select . . .
from a left join
     b
     on . . . left join
     c
     on . . .
where c.? is not null or b.? is not null;

I finally found how to do that... And it is very easy and fast:我终于找到了怎么做……而且非常简单快捷:

SELECT ... FROM A
INNER JOIN B ON ....

UNION

SELECT ... FROM A 
INNER JOIN C ON ....

You got it.你说对了。 BTW, your picture is not correct, I think.顺便说一句,我认为你的图片不正确。 If you are looking for records which are part of A and part of B "OR" C, the circles B and C should be without intersection.如果您要查找属于 A 的一部分和属于 B“OR”C 的一部分的记录,则圆 B 和 C 应该没有交集。 You draw records which are part of A and part of B "AND" C instead.您改为绘制属于 A 的一部分和 B “AND” C 的一部分的记录。 If you draw it correctly, you'll see your final union result clearly.如果绘制正确,您会清楚地看到最终的并集结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM