日期之间的SQL语法

时间:2013-02-19 19:11:49

标签: sql date between

我有一个查询需要返回ShipDate在一系列日期之间的记录。以下查询有效:

$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之间返回记录的正确语法是什么?

1 个答案:

答案 0 :(得分:3)

假设您使用普通日期,则必须引用它们。

e.g。

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

不正确,因为2013-01-01是一个减法,你实际上在做

ShipDate BETWEEN 2011 AND 1992

ShipDate BETWEEN '$todayMinus' AND '$todayPlus'