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

2022-05-15 0 434

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获取指定时间段中的日期与月份内容请搜索NICE源码以前的文章或继续浏览下面的相关文章希望大家以后多多支持NICE源码!

免责声明:
1、本网站所有发布的源码、软件和资料均为收集各大资源网站整理而来;仅限用于学习和研究目的,您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。 不得使用于非法商业用途,不得违反国家法律。否则后果自负!

2、本站信息来自网络,版权争议与本站无关。一切关于该资源商业行为与www.niceym.com无关。
如果您喜欢该程序,请支持正版源码、软件,购买注册,得到更好的正版服务。
如有侵犯你版权的,请邮件与我们联系处理(邮箱:skknet@qq.com),本站将立即改正。

NICE源码网 MySql mysql获取指定时间段中所有日期或月份的语句(不设存储过程,不加表) https://www.niceym.com/37849.html