Mysql sql:在同一个键上左键连接多个文件
鉴于以下三个表格: 表1Mysql sql:在同一个键上左键连接多个文件,mysql,sql,left-join,Mysql,Sql,Left Join,鉴于以下三个表格: 表1 id name observation -------------------------- 1 mario serial 2 samantha drogue dealer 3 jennifer prostitute 4 megan nun 表2 id person_id contact ------------------------- 1 2 jefferson 表3 id
id name observation
--------------------------
1 mario serial
2 samantha drogue dealer
3 jennifer prostitute
4 megan nun
表2
id person_id contact
-------------------------
1 2 jefferson
表3
id person_id salary
---------------------
1 2 180 000
2 4 0
我希望输出是
id name observation contact salary
-------------------------------------------------
1 mario serial NULL NULL
2 samantha drogue dealer jefferson 180 000
3 jennifer prostitute NULL NULL
4 megan nun NULL 0
我应该在这里多次使用左连接吗?
如何做到这一点?您的主表是表1。第二个是联系和工资。通过从主节点到次节点进行左连接,意味着不管在右侧找到的匹配项如何,都要在左侧给我记录。联接基于辅助表的“person\u id”
你的数据库里有一些真正的坏蛋D
select
t1.id,
t1.name,
t1.observation,
t2.contact,
t3.salary
from
table1 t1
LEFT JOIN table2 t2
on t1.id = t2.person_id
LEFT JOIN table3 t3
on t1.id = t3.person_id