Mysql查詢時(shí)間區(qū)間日期列表實(shí)例代碼
1、查詢時(shí)間區(qū)間日期列表,不會(huì)由于數(shù)據(jù)表數(shù)據(jù)影響
select a.date from ( select curdate() - INTERVAL (a.a + (10 * b.a) + (100 * c.a) + (1000 * d.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 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 d ) a where a.date between '2020-01-20' and '2021-12-24' ORDER BY a.date asc
tips:如果要查詢當(dāng)前日期后面的數(shù)據(jù) curdate()改為截止日期就好
2、創(chuàng)建視圖可以公共使用
CREATE VIEW v_digits AS SELECT 0 AS digit 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; CREATE VIEW v_numbers AS SELECT ones.digit + tens.digit * 10 + hundreds.digit * 100 + thousands.digit * 1000 AS number FROM v_digits as ones, v_digits as tens, v_digits as hundreds, v_digits as thousands; -- 生成的日期格式為 yyyy-MM-dd CREATE VIEW v_dates AS SELECT SUBDATE(CURRENT_DATE(), number) AS date FROM v_numbers UNION ALL SELECT ADDDATE(CURRENT_DATE(), number + 1) AS date FROM v_numbers; -- 生成的日期格式為 yyyy-MM CREATE VIEW v_months AS SELECT DATE_FORMAT(SUBDATE(CURRENT_DATE(), INTERVAL number MONTH),'%Y-%m') AS date FROM v_numbers UNION ALL SELECT DATE_FORMAT(ADDDATE(CURRENT_DATE(), INTERVAL number+1 MONTH),"%Y-%m") AS date FROM v_numbers;
3、創(chuàng)建為視圖之后,可以通過視圖查詢時(shí)間區(qū)間列表日期
SELECT date FROM v_dates WHERE date BETWEEN '2020-01-20' AND '2021-01-24' ORDER BY date asc
4、查詢時(shí)間區(qū)間按月
select DATE_FORMAT(str_to_date (a.Date,'%Y-%m-%d'),"%Y-%m") as Date from ( select '2011-12-24' - INTERVAL (a.a + (10 * b.a) + (100 * c.a) + (1000 * d.a) ) MONTH 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 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 d ) a where a.Date between '2010-01-20' and '2011-12-24' ORDER BY a.Date asc;
附:在對(duì)mysql的時(shí)間進(jìn)行區(qū)間查詢的時(shí)候出現(xiàn)的問題
<if test="searchcondition.starttime!=null"> <![CDATA[ and select_data.data_time ?>= #{searchcondition.starttime,jdbcType=TIMESTAMP} ]]> </if> <if test="searchcondition.stoptime!=null"> <![CDATA[ and select_data.data_time <= #{searchcondition.stoptime,jdbcType=TIMESTAMP} ]]> </if>
在test中不能使用searchcondition.stoptime!=’ ‘這個(gè)判斷會(huì)報(bào)錯(cuò),上面的是標(biāo)準(zhǔn)的時(shí)間查詢,自己做的時(shí)候總是會(huì)加上!=’ ‘這個(gè)條件.所以總是報(bào)錯(cuò),記錄一下.
總結(jié)
到此這篇關(guān)于Mysql查詢時(shí)間區(qū)間日期列表的文章就介紹到這了,更多相關(guān)Mysql查詢時(shí)間區(qū)間日期列表內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- MySQL查詢優(yōu)化:LIMIT 1避免全表掃描提高查詢效率
- MySQL查詢結(jié)果復(fù)制到新表的方法(更新、插入)
- mysql查詢表里的重復(fù)數(shù)據(jù)方法
- Python中模塊pymysql查詢結(jié)果后如何獲取字段列表
- Mysql查詢表中最小可用id值的方法
- 使用pymysql查詢數(shù)據(jù)庫,把結(jié)果保存為列表并獲取指定元素下標(biāo)實(shí)例
- mysql查詢表是否被鎖的方法
- mysql查詢優(yōu)化之100萬條數(shù)據(jù)的一張表優(yōu)化方案
- MySQL實(shí)現(xiàn)查詢數(shù)據(jù)庫表記錄數(shù)
相關(guān)文章
DB為何大量出現(xiàn)select @@session.tx_read_only 詳解
這篇文章主要給大家介紹了關(guān)于DB為何大量出現(xiàn)select @@session.tx_read_only 的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-04-04Ubuntu?服務(wù)器安裝?MySQL?遠(yuǎn)程數(shù)據(jù)庫的方法
本篇介紹如何在 Linux 服務(wù)器上安裝 MySQL 數(shù)據(jù)庫,并設(shè)置為可遠(yuǎn)程連接,本文通過命令給大家介紹的非常詳細(xì),對(duì)Ubuntu?安裝?MySQL遠(yuǎn)程數(shù)據(jù)庫感興趣的朋友一起看看吧2022-08-08mysql類似oracle rownum寫法實(shí)例詳解
在本篇文章里小編給大家分享的是關(guān)于mysql類似oracle rownum寫法以及相關(guān)實(shí)例內(nèi)容,需要的朋友們可以學(xué)習(xí)下。2019-09-09CentOS 7搭建多實(shí)例MySQL8的詳細(xì)教程(想要幾個(gè)搞幾個(gè))
這篇文章主要介紹了CentOS 7搭建多實(shí)例MySQL8的詳細(xì)教程(想要幾個(gè)搞幾個(gè)),本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05Mysql在debian系統(tǒng)中不能插入中文的終極解決方案
在debian環(huán)境下,徹底解決mysql無法插入和顯示中文的問題,需要的朋友可以參考下2013-09-09允許遠(yuǎn)程用戶訪問mysql服務(wù)sql語句
本節(jié)主要介紹了如何允許遠(yuǎn)程用戶訪問mysql服務(wù),本例授權(quán)192.168.14.1 主機(jī)的cakephp用戶訪問cakephp數(shù)據(jù)庫2014-07-07