SELECT
datecreate AS period,
count(cod) as qt
FROM `tablename`
WHERE `date` between CURDATE() - INTERVAL -1 year and CURDATE()
GROUP BY period
+----------+-----+
| period | qt |
+----------+-----+
|2015-10 | 5 |
+----------+-----+
|2015-11 | 7 |
+----------+-----+
|2016-06 | 9 | < --- the query ends here
+----------+-----+
|2016-05 | 0 | < --- what i need
+----------+-----+
...
...
...
使用数据范围内未匹配的月份(并计数0)填写日期,因为表格中不存在2016-11及以上日期
我通常在服务器上做,但我认为可以通过SQL
来做到这一点