-1

看看这个:图片

我的 SQL 查询是:

SELECT @rownum := @rownum + 1 AS ID, SKU, SUM(Quantity) FROM orders, (SELECT @rownum := 0) r
WHERE ShipDate BETWEEN 01-05-2013 AND 11-05-2013 
GROUP BY SKU

在我的代码中,我正在执行以下操作:

$sql = "SELECT @rownum := @rownum + 1 AS ID, SKU, SUM(Quantity) 
FROM orders, (SELECT @rownum := 0) r
WHERE ShipDate BETWEEN {$from_date } AND  {$to_date }
GROUP BY SKU";

我想选择介于两个值之间的数据。

ShipDate是文本字段而不是日期字段。我检查了表,它保存了数据,但 MySQL 返回 null。

为什么?我做错了什么?

如何更新我的 PHP 代码?

这段代码:

"SELECT @rownum := @rownum + 1 AS ID, SKU, SUM(Quantity) 
FROM orders, (SELECT @rownum := 0) r
WHERE STR_TO_DATE(ShipDate,'%d-%m-%Y') BETWEEN {$from_date } AND  {$to_date }
GROUP BY SKU";

不适合我。

请帮我。

4

2 回答 2

3

使用STR_TO_DATE()将文本转换为日期

SELECT @rownum := @rownum + 1 AS ID, 
       SKU, 
       SUM(Quantity) 
FROM orders, (SELECT @rownum := 0) r
WHERE STR_TO_DATE(ShipDate,'%d-%m-%Y') BETWEEN '2013-05-01' AND '2013-05-11'
GROUP BY SKU

并在日期周围添加引号并将日期重新排列为yyyy-mm-dd

于 2013-05-10T18:30:09.230 回答
1

由于 ShipDate 是文本字段,因此BETWEEN在文本数据的情况下无法正确评估运算符,因此查询失败。使用将您的文本数据转换为日期STR_TO_DATE()

于 2013-05-10T18:33:45.073 回答