Reputation:
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];
Upvotes: 0
Views: 2258
Reputation: 21
Try this:
SELECT * FROM `orders` WHERE `orderdate` >= DATE_FORMAT(NOW(), '%Y-%m-01 00:00:00')
It's good practice to avoid functions on fields something like
where MONTH(orderdate)
because you will loose using of indexes.
Upvotes: 2
Reputation: 3867
I would change the SQL from
SELECT count(1) FROM orders WHERE MONTH = MONTH(CURDATE()) AND YEAR = YEAR(CURDATE())
To
SELECT count(*) FROM `orders` WHERE MONTH(`orderdate`) = MONTH(CURDATE()) AND YEAR(`orderdate`) = YEAR(CURDATE())
Where orderdate
is the field that records the date of the order ;)
Upvotes: 2