SQL Server INNER JOIN 具有多个关系的多个内部联接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9834574/
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 14:58:05  来源:igfitidea点击:

SQL Server INNER JOIN multiple inner joins with multiple relations

sqldatabasesql-server-2008

提问by nawlrus

I have the following query. It works just fine but I need to pull the BUserName from another table named FB that has a UserID field that is related to the UserID in the FU table. Is this possible?

我有以下查询。它工作得很好,但我需要从另一个名为 FB 的表中提取 BUserName,该表具有与 FU 表中的 UserID 相关的 UserID 字段。这可能吗?

    SELECT TOP 100 
    FF.XID, 
    FF.YID, 
    FF.Title, 
    FF.FileName, 
    FF.FilePath, 
    FU.UserName as FUUserName, 
    FU.UserName as BUserName
    FROM FF 
    INNER JOIN FU ON FU.UserID = FF.UserID 

Just to clarify. I dont have a UserName column in the FB table. It does have FB.UserID which has a relation to FF.UserID which is where I want to pull the second UserName from. So with that relation I am trying to pull the username down from the FF.UserID table that is related to the userID in the FB table. Does this make any sense?

只是为了澄清。我在 FB 表中没有 UserName 列。它确实有 FB.UserID,它与 FF.UserID 有关系,我想从中提取第二个 UserName。因此,通过这种关系,我试图从与 FB 表中的用户 ID 相关的 FF.UserID 表中提取用户名。这有道理吗?

回答by Justin Pihony

You want something like this:

你想要这样的东西:

SELECT TOP 100 
FF.XID, 
FF.YID, 
FF.Title, 
FF.FileName, 
FF.FilePath, 
FU.UserName as FUUserName, 
FU.UserName as BUserName,
FB.BUserName as FB_BUserName
FROM FF 
    INNER JOIN FU ON FU.UserID = FF.UserID 
    INNER JOIN FB ON FB.UserID = FU.UserID

Now, FFties to FU, which then ties to FB. As they are all inner joined, you can use the law of association to understand that this acts like they are all linked together.

现在,FF绑定到FU,然后绑定到FB。由于它们都是内部连接的,因此您可以使用关联定律来理解这就像它们都连接在一起一样。

 FF = FU   FU = FB    Therefore FF = FB

回答by Andrey Gurinov

Like that?

像那样?

SELECT TOP 100  
    FF.XID,  
    FF.YID,  
    FF.Title,  
    FF.FileName,  
    FF.FilePath,  
    FU.UserName as FUUserName,  
    FU.UserName as BUserName,
    FB.BUserName  
    FROM FF  
    INNER JOIN FU ON FU.UserID = FF.UserID  
    INNER JOIN FB ON FU.UserID = FB.UserID  

回答by juergen d

SELECT TOP 100 
FF.XID, 
FF.YID, 
FF.Title, 
FF.FileName, 
FF.FilePath, 
FU.UserName as FUUserName, 
FB.UserName as BUserName
FROM FF 
INNER JOIN FU ON FU.UserID = FF.UserID 
INNER JOIN FB ON FU.UserID = FB.UserID 

回答by Rohan Büchner

 SELECT TOP 100 
    FF.XID, 
    FF.YID, 
    FF.Title, 
    FF.FileName, 
    FF.FilePath, 
    FU.UserName as FUUserName, 
    FB.BUserName
    FROM FF 
    INNER JOIN FU ON FU.UserID = FF.UserID 
    INNER JOIN FB ON FB.UserID = FU.UserID

回答by Sherif Hamdy

Another two example

另外两个例子

1- with null result:

1- 结果为空:

use my_DB
SELECT tbl_users.UserName
,Updated.UserName   /*Updated not in tbl*/

FROM [my_DB].[dbo].[my_tbl]
left outer join  tbl_users ON tbl_users.UserID = my_tbl.UserID
left outer join tbl_users Updated ON tbl_users.UserID = my_tbl.LasUpdatedUserID

2- with out null result:

2- 没有空结果:

SELECT tbl_users.UserName
,Updated.UserName   /*Updated not in tbl*/
FROM [my_DB].[dbo].[my_tbl]
INNER JOIN  tbl_users ON tbl_users.UserID = my_tbl.UserID
INNER JOIN tbl_users Updated ON tbl_users.UserID = my_tbl.LasUpdatedUserID

**

**