我有一个查询,我每天凌晨3点运行,以便给我当月的销售额。
问题出现在每个月的最后一天。因为月份已经翻了。 因此,当我在11月1日凌晨3点运行时,它给出了0.而它应该给我10月份的销量。 这是查询。
SELECT COUNT(order_id) AS added_orders
FROM `order`
WHERE date_added > DATE_ADD(CURDATE(), INTERVAL -31 DAY)
AND order_status_id != 0
AND MONTH(date_added)= MONTH(curdate())
任何想法?
答案 0 :(得分:2)
SELECT COUNT(order_id) AS added_orders FROM `order`
where date_added > DATE_ADD(CURDATE(), INTERVAL -31 DAY) AND order_status_id != 0
AND MONTH(date_added)= MONTH(DATE_ADD(CURDATE(), INTERVAL -1 DAY))
我更改了MONTH(date_added)= MONTH(curdate()),因此它需要前一天的月份
答案 1 :(得分:0)
SELECT COUNT(order_id) AS added_orders
FROM `order`
WHERE date_added > DATE_ADD(CURDATE(), INTERVAL -31 DAY)
AND order_status_id != 0
AND MONTH(date_added)=Convert(datetime, DateAdd(month, -1, Convert(date, GetDate())));
答案 2 :(得分:-1)
您可以使用与31天前的任何月份相同的月份...
SELECT COUNT(order_id) AS added_orders
FROM `order`
WHERE date_added > DATE_ADD(CURDATE(), INTERVAL -31 DAY)
AND order_status_id != 0
AND MONTH(date_added)= MONTH(DATE_ADD(CURDATE(), INTERVAL -31 DAY))