【MySQL】通过SQL语句构造连续日期
通过SQL语句构造连续的日期,如:构造2020年一整年的日期。
1 2 3 4 5 |
select date_format(date_add('20200101',interval t.id day),'%Y-%m-%d') as 'my_date' from (select help_topic_id as id from mysql.help_topic order by help_topic_id) t where t.id < datediff('20210101','20200101'); |
如果想构造某一个时间段的连续日期,那么可以将上面SQL中的“变量”(20200101,20210101)改一下,改成你需要的时间段。如:2020年9月7日至年底,那么改造后如下:
1 2 3 4 5 |
select date_format(date_add('20200907',interval t.id day),'%Y-%m-%d') as 'my_date' from (select help_topic_id as id from mysql.help_topic order by help_topic_id) t where t.id< datediff('20210101','20200907'); |