我的问题是根据他们的肌酸的年和月从数据库中选择记录计数。我有这个MySQL查询:
SELECT year, m, month, COUNT(bans.ban_id) AS total
FROM (
SELECT year, m, month
FROM
(SELECT YEAR(CURDATE()) year UNION ALL SELECT YEAR(CURDATE())-1) years,
(
SELECT 1 m, 'Jan' month
UNION ALL SELECT 2 m, 'Feb' month
UNION ALL SELECT 3 m, 'Mar' month
UNION ALL SELECT 4 m, 'Apr' month
UNION ALL SELECT 5 m, 'May' month
UNION ALL SELECT 6 m, 'Jun' month
UNION ALL SELECT 7 m, 'Jul' month
UNION ALL SELECT 8 m, 'Aug' month
UNION ALL SELECT 9 m, 'Sep' month
UNION ALL SELECT 10 m, 'Oct' month
UNION ALL SELECT 11 m, 'Nov' month
UNION ALL SELECT 12 m, 'Dec' month
) months
) ym
LEFT JOIN bans
ON ym.year = YEAR(bans.ban_created_at)
AND ym.m = MONTH(bans.ban_created_at)
WHERE
(year=YEAR(CURDATE()) AND m<=MONTH(CURDATE()))
OR
(year<YEAR(CURDATE()) AND m>MONTH(CURDATE()))
AND
(ban_banlist_id = 13)
GROUP BY year, m
它几乎正常工作,但是 - 问题是查询只返回具有有效记录数的值 - 例如当月内没有记录时,不会返回。请问你能帮帮我吗? :)
谢谢,
TheKronnY
答案 0 :(得分:0)
原因是右表(禁令)上的条件在WHERE子句上,因此它适用于结果,其效果是只选择出现的月份。把它放在LEFT JOIN子句的ON子句中,如下所示:
SELECT year, m, month, COUNT(bans.ban_id) AS total
FROM (
SELECT year, m, month
FROM
(SELECT YEAR(CURDATE()) year UNION ALL SELECT YEAR(CURDATE())-1) years,
(
SELECT 1 m, 'Jan' month
UNION ALL SELECT 2 m, 'Feb' month
UNION ALL SELECT 3 m, 'Mar' month
UNION ALL SELECT 4 m, 'Apr' month
UNION ALL SELECT 5 m, 'May' month
UNION ALL SELECT 6 m, 'Jun' month
UNION ALL SELECT 7 m, 'Jul' month
UNION ALL SELECT 8 m, 'Aug' month
UNION ALL SELECT 9 m, 'Sep' month
UNION ALL SELECT 10 m, 'Oct' month
UNION ALL SELECT 11 m, 'Nov' month
UNION ALL SELECT 12 m, 'Dec' month
) months
) ym
LEFT JOIN bans
ON ym.year = YEAR(bans.ban_created_at)
AND ym.m = MONTH(bans.ban_created_at)
AND ban_banlist_id = 13
WHERE
(year=YEAR(CURDATE()) AND m<=MONTH(CURDATE()))
OR
(year<YEAR(CURDATE()) AND m>MONTH(CURDATE()))
GROUP BY year, m