I have a table with items and the date that they're added. What I want is to get a report of added items by year and month.
My query:
SELECT COUNT(*) FROM items
GROUP BY YEAR(FROM_UNIXTIME(items.added)), MONTH(FROM_UNIXTIME(items.added))
The query returns
+----------+
| COUNT(*) |
+----------+
| 45 |
| 22 |
| 8 |
| 12 |
| 27 |
+----------+
which is correct data, but i also want to get 0 from the months when there is no item added for the last 3 years. And also get the column name as YEAR - MONTH.
I tried SELECT IF(COUNT(*) IS NULL, 0, COUNT(*)) and also SELECT IFNULL(COUNT(*), 0) but none of them worked. I'm stuck.