简体   繁体   中英

SQL : Join of inner join

I am a beginner on 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.

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 inner join C on #the keys

and between A and B:

A inner join B on #the keys

My question is: how to join/add those two inner join?

I would use 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:

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. You draw records which are part of A and part of B "AND" C instead. If you draw it correctly, you'll see your final union result clearly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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