简体   繁体   中英

Join three tables A, B, C and return common in A in mysql

I want to join below three tables A, B, C and return only common(shaded) part of table A

A
-------
ID, Name

B
-------
ID, ORG

C
--------
ID, DEP

在此处输入图像描述

Please anyone provide simple join query

I understand that you want rows from a whose id can be foud in either b or c .

This sounds like two exists subquery:

select a.*
from a
where 
    exists (select 1 from b where b.id = a.id)
    or exists (select 1 from c where c.id = a.id)

If you also want columns from tables b or c, you can use two left joins instead, with a where condition that ensures that at least one of the joins did succeed:

select a.*, b.org, c.dept
from a
left join b on b.id = a.id
left join c on c.id = a.id
where b.id is not null or c.id is not null

You want a left join starting with A and then some filtering:

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

The ? are either columns used in the join s or primary keys on the respective tables.

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