2015-09-07 87 views
0

我有問題。執行以下SQL查詢時:日期選擇..不會工作

SELECT order_id, orderdatum, plaats 
FROM order_row 
INNER JOIN order_row_oneoff ON order_row.order_row_id = order_row_oneoff.order_row_oneoff_id 
INNER JOIN orders ON order_row.order_id = orders.ordernr 
WHERE orderdatum >= '2015-09-01' 
AND orderdatum < '2015-09-05' 
AND order_row.product_id = '4118' OR order_row.product_id = '4128' OR order_row.product_id = '4176' AND orders.orderstatus > 98 

它只是選擇所有內容並忽略日期。 但是,當您刪除第二個AND子句時,它會起作用。爲什麼?

+0

列orderdatum是否可以爲空? –

+1

哪個數據類型是列orderdatum? – facundofarias

+1

顯示您的表格定義以及預期的輸出 –

回答

1

and具有較高的operator precedence這意味着and結合強於or。用括號把條件一起

where orderdatum >= '2015-09-01' 
and orderdatum < '2015-09-05' 
and 
(
    order_row.product_id = '4118' 
    or order_row.product_id = '4128' 
    or order_row.product_id = '4176' 
) 
and orders.orderstatus > 98 

或在您的情況使用IN

where orderdatum >= '2015-09-01' 
and orderdatum < '2015-09-05' 
and order_row.product_id in ('4118', '4128' , '4176') 
and orders.orderstatus > 98 
+0

非常感謝!!!!有用!! – mickey

0

其實你在最後有一個問題,你應該使用,而不是ANDIN操作。

product_id IN ('4118', '4128' , '4176')