2013-02-19 65 views
0

我有一個查詢需要返回ShipDate在日期範圍之間的記錄。下面的查詢工作:日期之間的SQL語法

$query = "SELECT ECCustomerOrder.ShipDate, 
ECCustomers.Customer, 
ECCustomerOrder.PO, 
ECCustomerOrder.TotalCases, 
ECCustomerOrder.order_number 
FROM ECCustomerOrder INNER JOIN ECCustomers ON ECCustomerOrder.ID = ECCustomers.ID 
WHERE ECCustomerOrder.ShipDate >= $todayMinus"; 

$ todayMinus是一個PHP變量早些時候在接受當前日期減去15天腳本設置。我不能去工作如下:

$query = "SELECT ECCustomerOrder.ShipDate, 
ECCustomers.Customer, 
ECCustomerOrder.PO, 
ECCustomerOrder.TotalCases, 
ECCustomerOrder.order_number 
FROM ECCustomerOrder INNER JOIN ECCustomers ON ECCustomerOrder.ID = ECCustomers.ID 
WHERE ECCustomerOrder.ShipDate BETWEEN $todayMinus AND $todayPlus"; 

,這是什麼SQL查詢的正確語法爲$ todayMinus和$ todayPlus的ShipDates之間返回記錄?

+0

這是MySQL的?你需要包括今天的結果?爲什麼它不起作用? – Lamak 2013-02-19 19:13:08

回答

3

假設您使用的是明碼日期,您必須引用它們。

例如

ShipDate BETWEEN 2013-01-01 AND 2013-02-19 

是不正確的,因爲2013-01-01是減法,你實際上是在做

ShipDate BETWEEN 2011 AND 1992 

嘗試

ShipDate BETWEEN '$todayMinus' AND '$todayPlus' 
+0

啊,很好。 – iamnotmaynard 2013-02-19 19:16:48

+0

就是這些,Marc。謝謝你的提示。 – Sterve 2013-02-19 19:21:32