欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

mysql获取指定时间段中所有日期或月份的语句(不设存储过程,不加表)

程序员文章站 2022-03-11 12:53:45
mysql获取一个时间段中所有日期或者月份1:mysql获取时间段所有月份select date_format(date_add('2020-01-20 00:00:00', interval row...

mysql获取一个时间段中所有日期或者月份

1:mysql获取时间段所有月份

select date_format(date_add('2020-01-20 00:00:00', interval row month),'%y-%m') date from
 ( 
    select @row := @row + 1 as row from 
    (select 0 union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t,
    (select 0 union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t2, 
    (select @row:=-1) r
 ) se
 where date_format(date_add('2020-01-20 00:00:00', interval row month),'%y-%m') <= date_format('2020-04-02 00:00:00','%y-%m')

2:mysql获取时间段所有日期

select date_add('2020-01-20 00:00:00', interval row day) date from
 ( 
    select @row := @row + 1 as row from 
    (select 0 union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t,
    (select 0 union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t2, 
    (select @row:=-1) r
 ) se
 where date_add('2020-01-20 00:00:00', interval row day) <= '2020-03-02 00:00:00'

备注:

这段代码表示数据条数限制,写两次查询的日期最多显示100条,写三次查询日期最多显示1000次,以此类推,根据你自己的需求决定

下面是设置最多显示条数10000写法

mysql获取指定时间段中所有日期或月份的语句(不设存储过程,不加表)

希望能帮助到你,萌新在线求带!!!

下面是其他网友的补充大家可以参考一下

1、不使用存储过程,不使用临时表,不使用循环在mysql中获取一个时间段的全部日期

select a.date 
from (
    select curdate() - interval (a.a + (10 * b.a) + (100 * c.a)) day as date
    from (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as a
    cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as b
    cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as c
) a
where a.date between '2017-11-10' and '2017-11-15'

输出如下

date
----------
2017-11-15
2017-11-14
2017-11-13
2017-11-12
2017-11-11
2017-11-10

2、mysql获取两个日期内的所有日期列表

select @num:=@num+1,date_format(adddate('2015-09-01', interval @num day),'%y-%m-%d') as date
from btc_user,(select @num:=0) t where adddate('2015-09-01', interval @num day) <= date_format(curdate(),'%y-%m-%d')
order by date;

此方法优点就是不需要创建存储过程或者是日历表,缺点就是你必须要有一个表,它的数据条数大到足够支撑你要查询的天数

3、mysql获取给定时间段内的所有日期列表(存储过程)

delimiter $$
drop procedure if exists create_calendar $$
create procedure create_calendar (s_date date, e_date date)
begin
-- 生成一个日历表
set @createsql = ‘create table if not exists calendar_custom (
`date` date not null,
unique key `unique_date` (`date`) using btree
)engine=innodb default charset=utf8‘;
prepare stmt from @createsql;
execute stmt;
while s_date <= e_date do
insert ignore into calendar_custom values (date(s_date)) ;
set s_date = s_date + interval 1 day ;
end while ;
end$$
delimiter ;
-- 生成数据到calendar_custom表2009-01-01~2029-01-01之间的所有日期数据
call create_calendar (‘2009-01-01‘, ‘2029-01-01‘);
delimiter $$
drop procedure if exists create_calendar $$
create procedure create_calendar (s_date date, e_date date)
begin
-- 生成一个日历表
set @createsql = ‘truncate table calendar_custom‘;
prepare stmt from @createsql;
execute stmt;
while s_date <= e_date do
insert ignore into calendar_custom values (date(s_date)) ;
set s_date = s_date + interval 1 day ;
end while ;
end$$
delimiter ;
-- 生成数据到calendar_custom表2009-01-01~2029-01-01之间的所有日期数据
call create_calendar (‘2009-01-02‘, ‘2009-01-07‘);

到此这篇关于mysql获取指定时间段中所有日期或月份的语句(不设存储过程,不加表)的文章就介绍到这了,更多相关mysql获取指定时间段中的日期与月份内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!