SQL Server 多个 LEFT JOIN,一对多
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7686802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SQL Server Multiple LEFT JOIN, one-to-many
提问by oooo ooo
I am looking for a way to perform multiple joins from one source table to more than one table. Similar to the following:
我正在寻找一种方法来执行从一个源表到多个表的多个连接。类似于以下内容:
SELECT a.NAME, b.address, c.phone
FROM tblname a
LEFT JOIN tbladdress b ON a.nid = b.nid
I also want to perform a left join on the Telephone table tblPhone
at the same time:
我还想同时在 Telephone 表上执行左连接tblPhone
:
tblname a left join tblPhone c on a.PID = c.PID
Try as I might I can't see how to put this into one query.
尽我所能,我看不出如何将其放入一个查询中。
回答by RedFilter
You can simply repeat your JOIN
clauses as many times as is needed, e.g.:
您可以JOIN
根据需要简单地多次重复您的条款,例如:
SELECT a.NAME
,b.address
,c.phone
FROM tblname a
LEFT JOIN tbladdress b ON a.nid = b.nid
LEFT JOIN tblPhone c ON a.PID = c.PID
回答by michael667
SELECT a.name, b.address, c.phone
FROM tblname a
left join tbladdress b on a.nid = b.nid
left join tblPhone c on a.PID = c.PID;
回答by Sergey Kudriavtsev
SELECT a.name, b.address, c.phone
FROM (tblname a
left join tbladdress b on a.nid = b.nid) c
left join tblPhone d on c.PID=d.PID