0

我有一个查询,我每天凌晨 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())

有任何想法吗 ?

4

3 回答 3

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()),所以它需要前一天的月份

于 2013-10-31T09:13:02.993 回答
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())));
于 2013-10-31T09:18:45.550 回答
-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))
于 2013-10-31T09:12:06.400 回答