1.当前年份: 取当前月份之前的月份(包括当前月份)
with RECURSIVE months as (
SELECT IF(YEAR(NOW()) = '2022', MONTH(NOW()),12) AS month_one
union all
SELECT month_one - 1 from months where month_one != 1
)
select month_one from months ORDER BY month_one;
2.过去的年份: 得到12个月
with RECURSIVE months as (
SELECT IF(YEAR(NOW()) = '2021', MONTH(NOW()),12) AS month_one
union all
SELECT month_one - 1 from months where month_one != 1
)
select month_one from months ORDER BY month_one;