【发布时间】:2016-09-01 04:14:48
【问题描述】:
我有以下查询
SELECT count(*) as count, Month(created_at) as month
FROM products
WHERE marketplace_id=21
and status='counterfeit'
and created_at < Now()
and created_at > DATE_ADD(Now(), INTERVAL - 5 MONTH)
group by month(created_at)
返回结果为
+-------+-------+
| count | month |
+-------+-------+
| 410 | 1 |
| 174 | 2 |
| 301 | 3 |
| 329 | 4 |
| 141 | 12 |
+-------+-------+
如果一个月没有值,它根本不会返回它,但我希望为该月设置默认值 0。
我试过this link Return a default value if no rows found 和
Returning a value if no result
我不确定是我无法正确实现还是这不是我想要的
【问题讨论】:
-
可能会创建一个日历表并仅存储月份(12 行)并对该表进行左连接。