最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

mysql獲取指定時(shí)間段中所有日期或月份的語句(不設(shè)存儲(chǔ)過程,不加表)

 更新時(shí)間:2021年06月17日 23:44:42   作者:骨力  
最近需要用mysql獲取一個(gè)時(shí)間段中的所有月份,網(wǎng)上查都是要設(shè)置存儲(chǔ)過程或者加一個(gè)日期表的,不滿足我的需求,翻墻找資料加上自己試驗(yàn),如下代碼分享給大家

mysql獲取一個(gè)時(shí)間段中所有日期或者月份

1:mysql獲取時(shí)間段所有月份

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獲取時(shí)間段所有日期

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'

備注:

這段代碼表示數(shù)據(jù)條數(shù)限制,寫兩次查詢的日期最多顯示100條,寫三次查詢?nèi)掌谧疃囡@示1000次,以此類推,根據(jù)你自己的需求決定

下面是設(shè)置最多顯示條數(shù)10000寫法

希望能幫助到你,萌新在線求帶?。?!

下面是其他網(wǎng)友的補(bǔ)充大家可以參考一下

1、不使用存儲(chǔ)過程,不使用臨時(shí)表,不使用循環(huán)在Mysql中獲取一個(gè)時(shí)間段的全部日期

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獲取兩個(gè)日期內(nèi)的所有日期列表

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;

此方法優(yōu)點(diǎn)就是不需要?jiǎng)?chuàng)建存儲(chǔ)過程或者是日歷表,缺點(diǎn)就是你必須要有一個(gè)表,它的數(shù)據(jù)條數(shù)大到足夠支撐你要查詢的天數(shù)

3、mysql獲取給定時(shí)間段內(nèi)的所有日期列表(存儲(chǔ)過程)

DELIMITER $$
DROP PROCEDURE IF EXISTS create_calendar $$
CREATE PROCEDURE create_calendar (s_date DATE, e_date DATE)
BEGIN
-- 生成一個(gè)日歷表
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 ;
-- 生成數(shù)據(jù)到calendar_custom表2009-01-01~2029-01-01之間的所有日期數(shù)據(jù)
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
-- 生成一個(gè)日歷表
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 ;
-- 生成數(shù)據(jù)到calendar_custom表2009-01-01~2029-01-01之間的所有日期數(shù)據(jù)
CALL create_calendar (‘2009-01-02‘, ‘2009-01-07‘);

到此這篇關(guān)于mysql獲取指定時(shí)間段中所有日期或月份的語句(不設(shè)存儲(chǔ)過程,不加表)的文章就介紹到這了,更多相關(guān)mysql獲取指定時(shí)間段中的日期與月份內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

监利县| 庐江县| 莱州市| 台中县| 梅州市| 连城县| 呼伦贝尔市| 荣成市| 山阳县| 柳江县| 平安县| 宿迁市| 桑植县| 称多县| 沧州市| 德清县| 汉川市| 金阳县| 金寨县| 平原县| 伊吾县| 上蔡县| 商南县| 科尔| 翁牛特旗| 宝山区| 平阴县| 若羌县| 甘谷县| 石家庄市| 西青区| 桂林市| 武山县| 芷江| 永昌县| 互助| 阿拉善左旗| 乃东县| 永城市| 西林县| 左贡县|