I want to SELECT from my table the last 30 day records. My queries looks like this:
SELECT DATE(o_date) as date, count(id) AS sum FROM customers WHERE o_date BETWEEN DATE_SUB(CURDATE(), INTERVAL 30 DAY) AND NOW() GROUP BY o_date
Or this:
SELECT DATE(o_date) AS date, COUNT(id) AS sum FROM customers WHERE o_date >= DATE(NOW()) + INTERVAL -30 DAY GROUP BY DATE(o_date)
I want to create a list with dates and count of id-s.
But where I dont have any records in exact day, the query just skip that date. But I want to insert there a zero.
Example:
id | o_date |
---|---|
1 | 2021-11-23 |
2 | 2021-11-22 |
3 | 2021-11-20 |
4 | 2021-11-20 |
5 | 2021-11-19 |
6 | 2021-11-18 |
7 | 2021-11-18 |
The result will be this:
date | sum |
---|---|
2021-11-23 | 1 |
2021-11-22 | 1 |
2021-11-20 | 2 |
2021-11-19 | 1 |
2021-11-18 | 2 |
But where I dont have records like in this example in 2021-11-21 how can I insert to the sum 0?
Thank you!
UPDATE: I need this query for MariaDB.