我每个月都有一组帖子。现在我需要一个数组,其中包含每个月发布的帖子的总记录。我在 MySql 查询下尝试过,它工作正常,但我期待 0(零)几个月没有记录。这里它不返回 0。
我读到 COUNT() 不会返回“0”,那么我该如何实现呢?
我尝试了 IFNULL() 和 COALESCE() 但仍然得到相同的结果。请帮助解决这个问题。谢谢你......
SELECT
count(id) as totalRec
FROM ('post')
WHERE year(date) = '2013'
AND monthname(date) IN ('January', 'February', 'March')
GROUP BY year(date)-month(date)
ORDER BY 'date' ASC
得到结果:
+----------+
| totalRec |
+----------+
| 7 |
| 9 |
+----------+
预期结果(一月份没有帖子):
+----------+
| totalRec |
+----------+
| 0 |
| 7 |
| 9 |
+----------+
样本数据:
+----+---------------------+
| id | date |
+----+---------------------+
| 24 | 2012-12-16 16:29:56 |
| 1 | 2013-02-25 14:57:09 |
| 2 | 2013-02-25 14:59:37 |
| 4 | 2013-02-25 15:12:44 |
| 5 | 2013-02-25 15:14:18 |
| 7 | 2013-02-26 11:31:31 |
| 8 | 2013-02-26 11:31:59 |
| 10 | 2013-02-26 11:34:47 |
| 14 | 2013-03-04 04:39:02 |
| 15 | 2013-03-04 05:44:44 |
| 16 | 2013-03-04 05:48:29 |
| 19 | 2013-03-07 15:22:34 |
| 20 | 2013-03-15 12:24:43 |
| 21 | 2013-03-16 16:27:43 |
| 22 | 2013-03-16 16:29:28 |
| 23 | 2013-03-16 16:29:56 |
| 11 | 2013-03-17 11:35:12 |
+----+---------------------+