如何使用两个条件进行左连接(在子句上)

时间:2015-10-21 16:13:12

标签: sql sql-server sql-server-2008

当我写下面的查询时,该消息返回

Msg 512,Level 16,State 1,Line 1 子查询返回的值超过1。当子查询遵循=,!=,<,< =,>,> =或子查询用作表达式时,不允许这样做。

请如何解决。

SELECT  PD.ProductID, PD.DesignID, PDCI.ColorID, 
        PD.TotalPrice, PD.PictureName, P.ProductTitle, 
        P.Quantity, P.Availability, P.TodayDeal,
        P.NewArrival, P.Bestseller, P.Likes, 
        P.CreatedOn, D.Name, PDCI.ColorImage
FROM [Diwan].tbProductDesigns PD    
LEFT JOIN [Diwan].tbProducts P 
       ON P.ProductID = PD.ProductID
LEFT JOIN [Design].tbDesignCover D 
       ON D.DesignID = PD.DesignID
LEFT JOIN [Diwan].tbProductDesignColorImages PDCI 
       ON PDCI.ProductID = PD.ProductID 
      AND PDCI.DesignID = PD.DesignID
WHERE PD.DesignID = 2

来自评论:总价是使用此函数的计算列:

ALTER FUNCTION [dbo].[fnTotalProductPrice] (
  @ProductID BIGINT,
  @DesignID BIGINT
)
RETURNS MONEY
AS
BEGIN
  RETURN (
      SELECT P.Price + D.SellingPrice
      FROM Diwan.tbProductDesigns PD
      LEFT JOIN Diwan.tbProducts P ON PD.ProductID = P.ProductID
      LEFT JOIN Design.tbDesignCover D ON PD.DesignID = D.DesignID
      WHERE PD.ProductID = @ProductID
        AND PD.DesignID = @DesignID
      )
END

1 个答案:

答案 0 :(得分:0)

我不明白为什么这个功能是必要的:

SELECT  PD.ProductID, PD.DesignID, PDCI.ColorID, 
        (P.Price + D.SellingPrice) AS TotalPrice, PD.PictureName, P.ProductTitle, 
        P.Quantity, P.Availability, P.TodayDeal,
        P.NewArrival, P.Bestseller, P.Likes, 
        P.CreatedOn, D.Name, PDCI.ColorImage
FROM [Diwan].tbProductDesigns PD    
LEFT JOIN [Diwan].tbProducts P 
       ON P.ProductID = PD.ProductID
LEFT JOIN [Design].tbDesignCover D 
       ON D.DesignID = PD.DesignID
LEFT JOIN [Diwan].tbProductDesignColorImages PDCI 
       ON PDCI.ProductID = PD.ProductID 
      AND PDCI.DesignID = PD.DesignID
WHERE PD.DesignID = 2