2013-12-10 82 views
0

好吧,如果我有兩個表下面的select語句:添加左連接到兩個表的結果,選擇

SELECT shipments.ShipmentID, shipments.Name, shipments.Reference_No As [Reference Number], shipments.Qty, shipments.Status, 
     Shipment_Details.ISBN, Shipment_Details.Title, Shipment_Details.Author, Shipment_Details.Publisher, 
FROM shipments, Shipment_Details 
WHERE shipments.Shipment_No = Shipment_Details.Shipment_No AND shipments.Order_No = Shipment_Details.Order_No 
       AND (([email protected]_No)) 

我怎樣才能做一個左側有一個表連接稱爲ShippingSystemExport,當貨物。ShippingID在ShippingSystemExport和發貨中找到。狀態(從原始查詢)不是「已發貨」,然後具有shipents.ShipmentID返回爲「在運輸部門掃描」

+0

是否有任何理由在這裏使用C#標籤? – ChunLin

+0

如果您的條件是'ShippingID'存在'ShippingSystemExport'中,您爲什麼還要加入ShippingSystemExport?爲什麼不在這種情況下只使用'INNER JOIN'(假設'ShipmentID'是你加入的領域)? –

回答

0

如果我正確理解您的要求,以下查詢應該完成這項工作。如果我錯了,請糾正我。

SELECT 'Scanned in Shipping Department' AS ShipmentID, shipments.Name, shipments.Reference_No As [Reference Number], shipments.Qty, shipments.Status, 
    Shipment_Details.ISBN, Shipment_Details.Title, Shipment_Details.Author, Shipment_Details.Publisher, 
FROM shipments 
INNER JOIN Shipment_Details ON shipments.Shipment_No = Shipment_Details.Shipment_No AND shipments.Order_No = Shipment_Details.Order_No AND (([email protected]_No)) 
LEFT JOIN ShippingSystemExport ON ShippingSystemExport.ShipmentID = shipments.ShipmentID 
WHERE shipments.Status <> 'Shipped' 

但是,爲什麼「Shippinged Scanned in Shipping Department」作爲ShipmentID返回?只是不知道...

0

這可能是你在找什麼:

SELECT a.ShipmentID, a.Name, a.Reference_No As [Reference Number], a.Qty, a.Status, 
b.ISBN, b.Title, b.Author, b.Publisher, a.ShipmentID AS [Scanned in Shipping Department] 
FROM shipments a 
INNER JOIN Shipment_Details b 
ON a.Shipment_No=b.Shipment_No AND a.Order_No = b.Order_No AND 
INNER JOIN ShippingSystemExport c 
ON a.ShipmentID=c.ShipmentID AND a.Status <> 'Shipped' 
WHERE [email protected]_No AND c.ShipmentID 

我用了一個INNER JOIN代替LEFT JOIN就像我在你的問題下我的意見陳述。