MYSQL 输入2个自定义年月 生成连续年月日期数据

发布于:2022-11-28 ⋅ 阅读:(828) ⋅ 点赞:(0)

1.输入开始月份和结束月份  生成连续的月份
 
select t3.cdate from (    
select  date_format(adddate('1970-01-01', interval t2.i * 100 + t1.i * 10 + t0.i month),'%Y-%m') `cdate`
from (select 0 i union select 1 union
      select 2 union select 3 union
      select 4 union select 5 union
      select 6 union select 7 union
      select 8 union select 9) t0,
     (select 0 i union select 1 union
      select 2 union select 3 union
      select 4 union select 5 union
      select 6 union select 7 union
      select 8 union select 9) t1,
     (select 0 i union select 1 union
      select 2 union select 3 union
      select 4 union select 5 union
      select 6 union select 7 union
      select 8 union select 9) t2 ) t3
  Where t3.cdate between '2022-01' and '2022-09'
  


  
 
  2. 生成连续 当前时间 往前推 12个月 月份数据
  
        SELECT
        date_format( date_add(curdate(), INTERVAL ( cast( help_topic_id AS signed ) - 12 ) MONTH ), '%Y-%m' ) AS cday
        FROM
        mysql.help_topic
        WHERE
        help_topic_id  <= 12
        ORDER BY
        help_topic_id DESC

 

 
 


网站公告

今日签到

点亮在社区的每一天
去签到