1、根据某表查询,但是要求表内的数据大于31行 (‘2023-04’ 是可替换的日期参数)
select @i := @i+1 as 'num',
date_add(date_format(concat('2023-04','-01'),'%Y-%m-%d'),interval @i day) as date1
from
table,
(select @i := -1) t
where
@i < (select dayofmonth(last_day(concat('2023-04','-01'))) -1)
2、如果某表内数据不足31行,可以用下面的方法(time01和time02的乘积,就是范围)
select @i := @i+1 as 'num',
-- date_add(date_format(concat('2023-04','-01'),'%Y-%m-%d'),interval @i day) as date1 此行和下面sql语句是一样的作用
DATE(date_add(concat('2023-04','-01'),interval @i day)) as date1
from
(select 1 union select 2 union select 3 union select 4 union select 5 union select 6) time01,
(select 1 union select 2 union select 3 union select 4 union select 5 union select 6) time02,
(select @i := -1) t
where
-- @i < (select dayofmonth(last_day(concat('2023-04','-01'))) -1) 此行和下面sql语句是一样的作用
@i < DATEDIFF(last_day(concat('2023-04','-01')),concat('2023-04','-01'))
3、查询某年中的12个月 ('2023'是替换的日期参数,time01*time02 == 3*4 )
select @i := @i+1 as 'num',
concat('2023','-',if(@i < 9,'0',''),@i+1) as month01
from
(select 1 union select 2 union select 3 ) time01,
(select 1 union select 2 union select 3 union select 4 ) time02,
(select @i := -1) t
where
@i < 11;