您好,登錄后才能下訂單哦!
本篇內容主要講解“mysql怎么獲取指定時間段中所有日期或月份”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“mysql怎么獲取指定時間段中所有日期或月份”吧!
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寫法
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怎么獲取指定時間段中所有日期或月份”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。