简体   繁体   English

连接三个表A、B、C并返回mysql中A中的common

[英]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、B、C 并只返回表 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 .我知道您想要来自a的行,其id可以在bc中找到。

This sounds like two exists subquery:这听起来像两个exists子查询:

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:如果您还想要表 b 或 c 中的列,则可以使用两个left joins ,其中的where条件可确保至少有一个连接成功:

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:您想要一个以A开头的left join ,然后进行一些过滤:

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.要么是join中使用的列,要么是各个表上的主键。

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

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