我们说我有一张如下表:
date_upload|url_upload|status_upload
2017-11-01 |www.com |verified
2017-12-01 |www.com |verified
2017-13-01 |www.com |verified
2017-11-01 |www.com |verified
我希望得到这样的结果:
date_upload |count_upload
2017-01-01 | 0
2017-02-01 | 0
.......... | 0
2017-11-01 | 2
2017-12-01 | 1
2017-13-01 | 1
2017-14-01 | 0
.......... | 0
2017-31-01 | 0
结果应该返回本月每个日期的计数,尽管我的记录只有4个。
答案 0 :(得分:0)
使用日历表:
SELECT t1.date, COUNT(t2.date_upload) AS count_upload
FROM
(
SELECT '2017-01-01' AS date UNION ALL
SELECT '2017-01-02' UNION ALL
SELECT '2017-01-03' UNION ALL
...
SELECT '2017-01-31'
) t1
LEFT JOIN yourTable t2
ON t1.date = t2.date_upload
GROUP BY
t1.date;
在实践中,名为t1
的子查询可以替换为包含这些日期的真实日历表。