2014-09-26 22 views
0

我有以下查詢....不能讓我的SQL查詢,以符合最新要求

SELECT DISTINCT 
    p.ProductID, p.ProductName, 
    od.Quantity, o.Shipcountry, o.ShippedDate 
FROM 
    Products p, [Order Details] od, Orders o 
WHERE 
    p.ProductID = od.ProductID 
    AND od.OrderID = o.OrderID 
    AND o.ShippedDate < '1997-01-01' 
    AND ShipCountry = 'Spain' 
    OR ShipCountry = 'Portugal' 
ORDER BY 
    p.ProductName ASC 

我試圖讓所有訂單按時發貨之前輸入的日期,但它不似乎工作......在所有的,下面是從查詢

82 Alice Mutton 91 Portugal 1996-10-17 00:00:00.000 
82 Alice Mutton 110 Portugal 1997-03-04 00:00:00.000 
17 Alice Mutton 21 Portugal 1996-11-18 00:00:00.000 
82 Alice Mutton 4 Portugal 1997-03-04 00:00:00.000 
17 Alice Mutton 28 Portugal 1998-04-13 00:00:00.000 
82 Alice Mutton 130 Portugal 1997-04-08 00:00:00.000 
17 Alice Mutton 10 Portugal 1996-10-25 00:00:00.000 
82 Alice Mutton 22 Portugal 1996-10-25 00:00:00.000 
17 Alice Mutton 1 Portugal 1997-06-06 00:00:00.000 
17 Alice Mutton 36 Portugal 1998-03-26 00:00:00.000 
82 Alice Mutton 49 Portugal 1997-01-02 00:00:00.000 
82 Alice Mutton 66 Portugal 1997-07-29 00:00:00.000 
82 Alice Mutton 39 Portugal 1997-01-02 00:00:00.000 
82 Alice Mutton 120 Portugal 1997-04-08 00:00:00.000 
+0

你的問題被標記的MySQL但您使用方括號,這是指示性的SQL Server或Sybase的。你真的在使用哪個數據庫? – 2014-09-26 02:22:34

+1

我的合同...其SQL Server – Jeremy 2014-09-26 02:26:46

+0

[踢壞的習慣:使用舊式JOIN](http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10/08/bad-habits-to-kick -using-old-style-joins.aspx) - 在ANSI-** 92 ** SQL標準(*)中,舊式*逗號分隔的表*樣式列表已替換爲* proper * ANSI'JOIN'語法*超過20年**),其使用不鼓勵 – 2014-09-26 05:30:54

回答

3

一對夫婦的結果OR聲明是搞亂你的結果在你的WHERE標準。您可以IN或使用括號解決這個問題:

SELECT DISTINCT p.ProductID, p.ProductName, od.Quantity,o.Shipcountry,o.ShippedDate 
FROM Products p 
    JOIN [Order Details] od ON p.ProductID=od.ProductID 
    JOIN Orders o ON od.OrderID=o.OrderID 
WHERE o.ShippedDate<'1997-01-01' 
    AND o.ShipCountry IN ('Spain','Portugal') 
ORDER BY p.ProductName ASC 

如果您願意使用括號,那麼這將是等效採用IN

... 
    AND (ShipCountry = 'Spain' OR ShipCountry = 'Portugal') 
... 
+0

@Jeremy - np,很高興提供幫助。 – sgeddes 2014-09-26 02:32:32

0

的問題是or聲明。但是,您應該使用顯式連接來重寫查詢。此外,IN將有助於解決與or問題:

SELECT DISTINCT p.ProductID, p.ProductName, od.Quantity,o.Shipcountry,o.ShippedDate 
FROM Products p JOIN 
    [Order Details] od 
    ON p.ProductID = od.ProductID IN 
    Orders o 
    ON od.OrderID=o.OrderID 
WHERE o.ShippedDate<'1997-01-01' AND 
    ShipCountry IN ('Spain', 'Portugal') 
ORDER BY p.ProductName ASC; 
0

按照mysql order of operationsAND優先OR所以使用括號:

SELECT DISTINCT p.ProductID, p.ProductName, od.Quantity,o.Shipcountry,o.ShippedDate 
FROM Products p, [Order Details] od, Orders o 
WHERE p.ProductID=od.ProductID 
    AND od.OrderID=o.OrderID 
    AND o.ShippedDate<'1997-01-01' 
    AND (ShipCountry = 'Spain' OR ShipCountry = 'Portugal') 
ORDER BY p.ProductName ASC 

我希望幫助;如果沒有,請留言。

0

您需要使用DATE_FORMAT()或日期()當您比較日期時間

SELECT DISTINCT p.ProductID, p.ProductName, od.Quantity,o.Shipcountry,o.ShippedDate 
    FROM Products p 
     JOIN [Order Details] od ON p.ProductID=od.ProductID 
     JOIN Orders o ON od.OrderID=o.OrderID 
    WHERE DATE_FORMAT(o.ShippedDate, "%Y-%m-%d") < '1997-01-01' 
     AND o.ShipCountry IN ('Spain','Portugal') 
    ORDER BY p.ProductName ASC