SQL SELECT语句

时间:2018-07-19 10:21:39

标签: sql sql-server

我正在尝试制作一个Callback,但是由于它总是给我错误,所以我无法在SQL Management Studio上对其进行测试。

我找不到错误,感谢您的帮助,谢谢!

SELECT

3 个答案:

答案 0 :(得分:3)

您的查询混合了隐式联接和显式联接。 永远不要使用隐式连接。

正确的查询结构如下:

SELECT [Order].CustomOrderNumber, 
       [Order].OrderTotal, 
       [Order].OrderDiscount, 
       [DiscountUsageHistory].DiscountID, 
       [DiscountUsageHistory].OrderID, 
       [Discount].ID, 
       [Discount].Name 
FROM [Order] 
INNER JOIN [DiscountUsageHistory] ON [Order].CustomOrderNumber = [DiscountUsageHistory].OrderID
INNER JOIN [Discount] ON [DiscountUsageHistory].DiscountID = [Discount].ID 
WHERE OrderDiscount > 0

答案 1 :(得分:0)

如果您的关系正确-

您有多个链接到from的表。

内部联接为您建立了这些关系。

    SELECT [Order].CustomOrderNumber, [Order].OrderTotal, [Order].OrderDiscount, [DiscountUsageHistory].DiscountID, [DiscountUsageHistory].OrderID, [Discount].ID, [Discount].Name 
    FROM [Order] 
    INNER JOIN [DiscountUsageHistory] ON [Order].CustomOrderNumber = [DiscountUsageHistory].OrderID
    INNER JOIN [Discount] ON [DiscountUsageHistory].DiscountID = [Discount].ID 
    WHERE [Order].OrderDiscount > 0

答案 2 :(得分:0)

尝试:

SELECT 
    ORD.CustomOrderNumber, 
    ORD.OrderTotal, 
    ORD.OrderDiscount,
    DISUH.DiscountID,
    DISUH.OrderID,
    DIS.ID, 
    DIS.[Name]

FROM 
    [Order] AS ORD  
INNER JOIN 
    [DiscountUsageHistory] AS DISHU ON ORD.CustomOrderNumber = DISHU.OrderID
INNER JOIN 
    [Discount]  AS DIS ON DISHU.DiscountID = DIS.ID 
WHERE ORD.OrderDiscount > 0