SQL Server INNER JOIN多个内部联接与多个关系 [英] SQL Server INNER JOIN multiple inner joins with multiple relations

查看:137
本文介绍了SQL Server INNER JOIN多个内部联接与多个关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下查询。它工作正常,但我需要从名为FB的另一个表,有一个UserID字段与FU表中的UserID相关的表中拉BUserName。这是否有可能?

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?

    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 

只是为了澄清。我没有在FB表中的UserName列。它有FB.UserID它与FF.UserID的关系,这是我想从中拉第二个UserName。所以使用这种关系,我试图从FF.UserID表中拉出用户名,该表与FB表中的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?

推荐答案

您要这样做:

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

现在, FF 绑定到 FU ,然后绑定到 FB

Now, FF ties 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   FU = FB    Therefore FF = FB

这篇关于SQL Server INNER JOIN多个内部联接与多个关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆