我正在按表TEST
的订单号检索所有记录和列,其中对于每个订单号,列MAX
的orderDeliveryDate
等于昨天。以下代码不起作用:
SELECT T.*
FROM TEST T
INNER JOIN(
SELECT orderNum, MAX(orderDeliveryDate) AS maxDeliveryDate
FROM TEST
GROUP BY orderNum) MX
ON T.orderNum= MX.orderNum
AND T.orderDeliveryDate = MX.maxDeliveryDate
AND MX.maxDeliveryDate = DATE_SUB(curdate(),INTERVAL 1 DAY)
最佳答案
应该这样做:
SELECT * FROM TEST
WHERE ORDERNUM IN
(SELECT ORDERNUM, MAX(orderDeliveryDate)
FROM TEST GROUP BY ORDERNUM
HAVING DATE(MAX(orderDeliveryDate)) = DATE(DATE_SUB(curdate(),INTERVAL 1 DAY)))
我正在猜测的是orderDeliveryDate是DateTime,因此将其转换为Date将解决您的问题。您的原始查询可以将日期转换为日期,我想我的说法要简单得多。