select t1.monthTime,t2.month_time ,t2.p_output from
(SELECT DATE_FORMAT( @cdate := DATE_ADD( @cdate, INTERVAL - 1 MONTH ) , '%Y-%m' ) as monthTime
FROM (SELECT @cdate := DATE_ADD( CURDATE(), INTERVAL 1 MONTH ) FROM `temp` LIMIT 12 ) as t) as t1
left join
(select IFNULL(sum(planned_output),0) as p_output ,date_format(start_time,'%Y-%m') as month_time from `schedule` where YEAR(start_time) = 2023 GROUP BY month_time) as t2 on t1.monthTime = t2.month_time
temp表,这里只要记录总数大于12的什么表都行
上面一条sql是把年份的12条记录查出来,如果是今年,比如说目前11月份,那么limit 11 即可。这个可能程序做个限制。
如果要统计去年,那么吧CURDATE()换成去年12月份的日子即可
下面一条是具体业务逻辑