希望这可以帮助..
SELECT
t1.month_year AS month,
COALESCE(SUM(t1.total+t2.total), 0) AS count
FROM
(
SELECT
DATE_FORMAT(a.Date, "%Y-%m") AS md,
DATE_FORMAT(a.Date, "%b-%y") AS month_year,
'0' AS total
FROM (
SELECT curdate() - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY AS Date
FROM (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS a
CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS b
CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS c
) a
WHERE a.Date <= NOW() AND a.Date >= Date_add(Now(),INTERVAL - 11 MONTH)
GROUP BY md
)t1
LEFT JOIN
(
SELECT DATE_FORMAT(created_at, "%b") AS month, COUNT(*) AS total ,DATE_FORMAT(created_at, "%Y-%m") AS md
FROM reports
WHERE created_at <= NOW() AND created_at >= Date_add(Now(),INTERVAL - 11 MONTH) AND `status` = 1
GROUP BY md
)t2
ON t2.md = t1.md
LEFT JOIN
(
SELECT DATE_FORMAT(date_lm, "%b") AS month, COUNT(*) AS total ,DATE_FORMAT(date_lm, "%Y-%m") AS md
FROM tbl_users
WHERE date_lm <= NOW() AND date_lm >= Date_add(Now(),INTERVAL - 11 MONTH) AND status = 'submitted'
GROUP BY md
)t3
ON t3.md = t1.md
GROUP BY t1.md
ORDER BY t1.md
这将提供过去 12 个月的数据,即使没有数据为 0..