0

I want to display the number of orders of current month and I don't know the query to do it:

This is the code:

$stats_orders_curr = mysql_query(
                     'SELECT count(1) FROM orders 
                      WHERE MONTH = MONTH(CURDATE()) 
                      AND YEAR = YEAR(CURDATE())'
);

$orders_curr =  mysql_fetch_array($stats_orders_curr);
$orderscurr = $orders_curr[0];
4

2 回答 2

2

尝试这个:

SELECT * FROM `orders` WHERE `orderdate` >=  DATE_FORMAT(NOW(), '%Y-%m-01 00:00:00')

避免在字段上使用函数是一种很好的做法,例如

where MONTH(orderdate)

因为您将失去对索引的使用。

于 2013-06-15T21:18:36.230 回答
2

我会从

SELECT count(1) FROM orders WHERE MONTH = MONTH(CURDATE()) AND YEAR = YEAR(CURDATE())

SELECT count(*) FROM `orders` WHERE MONTH(`orderdate`) = MONTH(CURDATE()) AND YEAR(`orderdate`) = YEAR(CURDATE())

orderdate记录订单日期的字段在哪里;)

于 2013-06-15T20:50:54.170 回答