SELECT t1.m as 'month',
IFNULL(t2.count,0) AS 'count'
FROM
(
select concat((select year(now())),'01') as 'm'
union select concat((select year(now())),'02')
union select concat((select year(now())),'03')
union select concat((select year(now())),'04')
union select concat((select year(now())),'05')
union select concat((select year(now())),'06')
union select concat((select year(now())),'07')
union select concat((select year(now())),'08')
union select concat((select year(now())),'09')
union select concat((select year(now())),'10')
union select concat((select year(now())),'11')
union select concat((select year(now())),'12')
) t1
LEFT JOIN
(
SELECT
DATE_FORMAT((表的时间),'%Y%m')as m,
count(需要coun的列) as count
FROM
你的表名 t
WHERE
查询条件
GROUP BY DATE_FORMAT(表的时间列,'%Y%m')
)t2
ON t1.m=t2.m
查询12个月的数据 没有就补0
于 2022-05-30 10:19:52 首次发布