簡體   English   中英

連接三個表A、B、C並返回mysql中A中的common

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

我想加入下面三個表 A、B、C 並只返回表 A 的公共(陰影)部分

A
-------
ID, Name

B
-------
ID, ORG

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

在此處輸入圖像描述

請任何人提供簡單的加入查詢

我知道您想要來自a的行,其id可以在bc中找到。

這聽起來像兩個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)

如果您還想要表 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

您想要一個以A開頭的left join ,然后進行一些過濾:

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

? 要么是join中使用的列,要么是各個表上的主鍵。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM