MySQL查询GROUP BY YEAR / MONTH并显示“未链接”日期以供选择

时间:2016-06-13 19:29:40

标签: mysql sql date

我有查询

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

来做到这一点

0 个答案:

没有答案