左联接到左联接

时间:2019-01-14 06:23:00

标签: sql join

以下代码可以正常工作:

SELECT *    -- * only for clarity
FROM
   (    SELECT      dbo.Contracts.buyerID AS PreviousContractPartyIDs 
        FROM        dbo.Contracts 
        WHERE       dbo.Contracts.sellerID = @UserID 
        GROUP BY    dbo.Contracts.buyerID 

        UNION

        SELECT      dbo.Contracts.sellerID 
        FROM        dbo.Contracts 
        WHERE       dbo.Contracts.BuyerID = @UserID 
        GROUP BY    dbo.Contracts.sellerID 
   ) AS ListOfAllMembersPreviouslyTradedWith

LEFT JOIN 

  ( SELECT  dbo.Members.ID, 
            dbo.Members.Name, 
            dbo.Members.Description 
    FROM    dbo.Members 
  ) AS MemberDetails 

ON ListOfAllMembersPreviouslyTradedWith.PreviousContractPartyIDs = MemberDetails.ID

顶部选择与该会员进行交易的所有用户,底部选择添加有关每个用户的详细信息。但是该成员可能在tbl中添加了有关每个用户的注释。但是我无法找出synatx /结构来添加另一个联接,基本上如下:

LEFT JOIN 
( SELECT MemberID, NoteAboutMemberID, Note, UTC from dbo.Notes ) AS NoteDetails     

ON ListOfAllMembersPreviouslyTradedWith.PreviousContractPartyIDs = NoteDetails.NoteAboutMemberID 

2 个答案:

答案 0 :(得分:0)

我认为您需要在下面

    SELECT ListOfAllMembersPreviouslyTradedWith.*,
            MemberDetails.*,
               NoteDetails.*
    FROM
       (    SELECT      distinct dbo.Contracts.buyerID AS PreviousContractPartyIDs 
            FROM        dbo.Contracts 
            WHERE       dbo.Contracts.sellerID = @UserID 


            UNION

            SELECT      distinct dbo.Contracts.sellerID 
            FROM        dbo.Contracts 
            WHERE       dbo.Contracts.BuyerID = @UserID 

       ) AS ListOfAllMembersPreviouslyTradedWith

    LEFT JOIN 

      ( SELECT  dbo.Members.ID, 
                dbo.Members.Name, 
                dbo.Members.Description 
        FROM    dbo.Members 
      ) AS MemberDetails 

    ON ListOfAllMembersPreviouslyTradedWith.PreviousContractPartyIDs = MemberDetails.ID
 LEFT JOIN 
( SELECT MemberID, NoteAboutMemberID, Note, UTC from dbo.Notes ) AS NoteDetails     

ON ListOfAllMembersPreviouslyTradedWith.PreviousContractPartyIDs = NoteDetails.MemberID AND MemberDetails.ID = NoteDetails.NoteAboutMemberID

答案 1 :(得分:0)

要考虑的重要事实

  1. 您的子查询ListOfAllMembersPreviouslyTradedWith在ID“ PreviousContractPartyIDs”中是否不同?

然后下面的代码就可以了,因为使用UNION语法子查询已经很明显了,因为没有聚合,所以可以避免使用group by。 最后离开,加入两个子查询。

SELECT *   
FROM
   (    SELECT      dbo.Contracts.buyerID AS PreviousContractPartyIDs 
        FROM        dbo.Contracts 
        WHERE       dbo.Contracts.sellerID = @UserID 


        UNION

        SELECT      dbo.Contracts.sellerID 
        FROM        dbo.Contracts 
        WHERE       dbo.Contracts.BuyerID = @UserID 

   ) AS ListOfAllMembersPreviouslyTradedWith

LEFT JOIN 

  ( SELECT DISTINCT  dbo.Members.ID, 
            dbo.Members.Name, 
            dbo.Members.Description 
    FROM    dbo.Members 
  ) AS MemberDetails 

ON ListOfAllMembersPreviouslyTradedWith.PreviousContractPartyIDs = MemberDetails.ID

LEFT JOIN 
( SELECT DISTINCT MemberID, NoteAboutMemberID, Note, UTC from dbo.Notes ) AS NoteDetails     

ON ListOfAllMembersPreviouslyTradedWith.PreviousContractPartyIDs = NoteDetails.NoteAboutMemberID