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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 12:22:17  来源:igfitidea点击:

SQL Server Multiple LEFT JOIN, one-to-many

sqljoin

提问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 tblPhoneat 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 JOINclauses 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