我有一张表,上面有项目和添加日期。我想要的是得到一份按年度和月份添加项目的报告。
我的问题:
SELECT COUNT(*) FROM items
GROUP BY YEAR(FROM_UNIXTIME(items.added)), MONTH(FROM_UNIXTIME(items.added))
查询返回
+----------+
| COUNT(*) |
+----------+
| 45 |
| 22 |
| 8 |
| 12 |
| 27 |
+----------+
这是正确的数据,但我也想得到 0
从最近3年内没有增加项目的月份开始。并将列名作为 YEAR - MONTH
.
我试过了 SELECT IF(COUNT(*) IS NULL, 0, COUNT(*))
还有 SELECT IFNULL(COUNT(*), 0)
但都没用。我卡住了。
2条答案
按热度按时间xwbd5t1u1#
试试这个:
68bkxrlz2#
这将适用于mysql工作台中的条件。