I'm trying to count the rows of each month of each year (there're a few years in a table) without skipping the empty month. Saying my table has some month without the data in it. Like 1,2,5,6,7,11,12 (3,4,8,9,10 is missing). But I don't want the output skip it - just put 0 instead.
I come up with this code but it skipping the empty record.
SELECT
YEAR(`log_datetime`) as year,MONTH(`log_datetime`) AS mon,
count(log_id) AS count
FROM
log_db
GROUP BY
MONTH (`log_datetime`),
YEAR (`log_datetime`)
ORDER BY
YEAR (`log_datetime`) DESC,
MONTH (`log_datetime`) DESC
What I expect is:
+------+----+-----+
| 2015 | 12 | 100 |
+------+----+-----+
| 2015 | 11 | 120 |
+------+----+-----+
| 2015 | 10 | 0 |
+------+----+-----+
| 2015 | 09 | 0 |
+------+----+-----+
| 2015 | 08 | 0 |
+------+----+-----+
| 2015 | 07 | 123 |
+------+----+-----+
| 2015 | 06 | 200 |
+------+----+-----+
| 2015 | 05 | 250 |
+------+----+-----+
| 2015 | 04 | 0 |
+------+----+-----+
| 2015 | 03 | 0 |
+------+----+-----+
| 2015 | 02 | 211 |
+------+----+-----+
| 2015 | 01 | 200 |
+------+----+-----+