使用显式 JOIN 逻辑将旧样式 SQL 语句转换为当前 SQL

Convert old style SQL statement to current SQL with explicit JOIN logic

我们的 EDI 应用程序使用 MySQL 后端来存储所有相关记录。 EDI 应用程序使用拖放式向导来创建 SQL 语句,因此可以创建的内容非常有限。这对于现有需求来说没问题,只是提取客户采购订单 header 和行信息。它只需要 header 和行 tables.

之间的简单连接
SELECT  
    "dbo"."Supplier live$Purchase Header"."Buy-from Vendor No_" AS CustInternalShipToRef,
    "dbo"."Supplier live$Purchase Header"."No_" AS CustPONumber,
    "dbo"."Supplier live$Purchase Line"."Line No_" AS POLineItemNumber,
    "dbo"."Supplier live$Purchase Line"."Vendor Item No_" AS IntProductCode,
    "dbo"."Supplier live$Purchase Line"."No_" AS CustomerProductCode,
    "dbo"."Supplier live$Purchase Line"."Quantity" AS Quantity,
    "dbo"."Supplier live$Purchase Line"."Unit of Measure" AS OrderQuantityUOM ,
    "dbo"."Supplier live$Purchase Line"."Expected Receipt Date" AS RequestedDeliveryDate,
    "dbo"."Supplier live$Purchase Line"."Direct Unit Cost" AS PricePerUnit,
FROM  
    "dbo"."Supplier live$Purchase Header",
    "dbo"."Supplier live$Purchase Line",
WHERE 
    ("dbo"."Supplier live$Purchase Header"."No_"="dbo"."Supplier live$Purchase Line"."Document No_") 
    AND ("dbo"."Supplier live$Purchase Header"."Buy-from Vendor No_" Like '%AJAX%') 
    AND ("dbo"."Supplier live$Purchase Header"."Order Date" >= getdate()-2) 
    AND ("dbo"."Supplier live$Purchase Header"."Status" = '1') 
    AND ("dbo"."Supplier live$Purchase Line"."Quantity" > '0')

现在我们需要包含第 3 个 table,这也可以用现有的 'wizard',但我们不能使用内部联接作为第 3 个 table第一个 table 中的每条记录都没有匹配的记录。所以现在我需要将旧样式 SQL 转换为当前语法并包含更明确的连接逻辑。

我想要实现的结果是根据 WHERE 部分中的标准显示 SELECT 部分中的所有现有字段,然后显示采购订单评论中的 header 评论 table 如果它们存在。

我一直在浏览这里的帖子,试图至少找到一个起点。因此,我整理了以下声明,并希望就我使用的逻辑是否会实现我在上面寻找的结果获得一些反馈。也不确定我是否应该只使用 LEFT JOIN 或 LEFT OUTER JOIN。

SELECT  
    "dbo"."Supplier live$Purchase Header"."Buy-from Vendor No_" AS CustInternalShipToRef,
    "dbo"."Supplier live$Purchase Header"."No_" AS CustPONumber,
    "dbo"."Supplier live$Purchase Line"."Line No_" AS POLineItemNumber,
    "dbo"."Supplier live$Purchase Line"."Vendor Item No_" AS IntProductCode,
    "dbo"."Supplier live$Purchase Line"."No_" AS CustomerProductCode,
    "dbo"."Supplier live$Purchase Line"."Quantity" AS Quantity,
    "dbo"."Supplier live$Purchase Line"."Unit of Measure" AS OrderQuantityUOM ,
    "dbo"."Supplier live$Purchase Line"."Expected Receipt Date" AS RequestedDeliveryDate,
    "dbo"."Supplier live$Purchase Line"."Direct Unit Cost" AS PricePerUnit,
    "dbo"."Supplier live$Purch_ Comment Line"."Comment" AS OrderComments
FROM  
    "dbo"."Supplier live$Purchase Header"
    LEFT JOIN (
        "dbo"."Supplier live$Purchase Header" INNER JOIN "dbo"."Supplier live$Purchase Line" ON "dbo"."Supplier live$Purchase Header"."No_"="dbo"."Supplier live$Purchase Line"."Document No_"
        AND "dbo"."Supplier live$Purchase Header"."Buy-from Vendor No_" Like '%AJAX%'
        AND "dbo"."Supplier live$Purchase Header"."Order Date" >= getdate()-2
        AND "dbo"."Supplier live$Purchase Header"."Status" = '1'
        AND "dbo"."Supplier live$Purchase Line"."Quantity" > '0'
    ) ON "dbo"."Supplier live$Purch_ Comment Line"."No_"="dbo"."Supplier live$Purchase Header"."No_"

我觉得你有点想多了。如果记录已经满足您在原始查询中的条件,请尝试将新的 table 添加为 LEFT JOIN。

Select t1.Col1 , t2.Col2 , nt1.Col3 
From Dbo.OriginalTable1 t1 
JOIN dbo.OriginalTable2 t2 ON t2.col1 =t1.col1
And t2.col2 =t1.col2 
LEFT JOIN dbo.NewTable1 nt1 On nt1.col1 = t1.col1

以上将 return 所有符合 tables 1 和 2 中的连接条件的记录。Col3 将在连接中存在匹配项的地方填充,在不匹配的地方填充 null .最后一个左连接不限制结果数据集。