[英]Joining three different tables based on one column name
table A
amount user_id
100 abc
200 cdf
300 def
table B
Idno user_id
10 abc
202 def
table C
Idno user_id
498 cdf
最終輸出
Idno user_id amount
10 abc 100
202 def 200
498 cdf 300
我知道要加入兩個表
select A.amount,B.Idno,B.user_id from B inner join A on A.user_id=B.user_id;
但是我的問題是如何在兩個表中顯示所有的user_id數量,如最終輸出所示
這是你想要的嗎?
SELECT a.amount, t.Idno, t.user_id FROM table_a a
JOIN (
SELECT Idno, user_id FROM table_b
UNION ALL
SELECT Idno, user_id FROM table_c
) t
ON a.user_id = t.user_id
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.