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

时间:2015-01-21 01:24:59

标签: mysql join

我们有和使用MySQL后端的EDI应用程序存储所有相关记录。 EDI应用程序使用拖放样式向导来创建SQL语句,因此它可以创建的内容非常有限。这适用于现有要求,即提取客户采购订单表头和行信息。它只需要在标题和行表之间进行简单连接。

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个表,这也可以使用现有的'向导',但是我们不能使用内连接,因为第3个表没有匹配的记录第一张表中的记录。所以现在我需要将旧样式SQL转换为当前语法并包含更明确的连接逻辑。

我想要实现的结果是按照WHERE部分中的条件显示SELECT部分​​中的所有现有字段,然后显示采购订单注释表中的标题注释(如果存在)。

我一直在寻找这里的帖子,试图获得至少一个起点。所以我把下面的陈述放在一起,并希望得到一些反馈,我所使用的逻辑是否会实现我正在寻找的结果。也不确定我是应该使用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_"

1 个答案:

答案 0 :(得分:1)

我认为你有点过分了。如果记录已满足原始查询中的条件,请尝试将新表添加为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

以上将返回满足表1和表2中的连接条件的所有记录。将在连接中存在匹配的位置填充Col3,而不存在其中的匹配。最后一个左连接不限制结果数据集。