mysql數(shù)據(jù)庫您要的常見日期查詢方法總結(jié)
一、前言
本文介紹了mysql常用日期查詢的方法。希望能幫助到您。
二、常見日期查詢方法
1、查詢今天數(shù)據(jù)
select * from 表名 where to_days(時間字段名) = to_days(now());
2、查詢昨天數(shù)據(jù)
select * from 表名 where to_days(now( )) - to_days( 時間字段名) <= 1
3、查詢近7天數(shù)據(jù)
select * from 表名 where date_sub(curdate(), interval 7 day) <= date(時間字段名)
4、查詢近30天數(shù)據(jù)
SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(時間字段名)
5、查詢當(dāng)前月數(shù)據(jù)
select * from 表名 where date_format( 時間字段名, '%y%m' ) = date_format( curdate() , '%y%m')
6、查詢上一月數(shù)據(jù)
select * from 表名 where period_diff( date_format( now( ) , '%y%m' ) , date_format( 時間字段名, '%y%m' ) ) =1
7、查詢本季度數(shù)據(jù)
select * from 表名 where quarter(create_date)=quarter(now());
8、查詢上季度數(shù)據(jù)
select * from 表名 where quarter(create_date)=quarter(date_sub(now(),interval 1 quarter));
9、查詢本年數(shù)據(jù)
select * from 表名 where year(create_date)=year(now());
10、查詢上年數(shù)據(jù)
select * from 表名 where year(create_date)=year(date_sub(now(),interval 1 year));
11、查詢當(dāng)前周的數(shù)據(jù)
select * from 表名 where yearweek(date_format(create_date,'%y-%m-%d')) = yearweek(now());
12、查詢上周的數(shù)據(jù)
select * from 表名 where yearweek(date_format(create_date,'%y-%m-%d')) = yearweek(now())-1;
13、查詢上個月的數(shù)據(jù)
select * from 表名 where date_format(create_date,'%y-%m')=date_format(date_sub(curdate(), interval 1 month),'%y-%m')
14、查詢當(dāng)前月份的數(shù)據(jù)
select * from 表名 where date_format(create_date,'%Y-%m')=date_format(now(),'%Y-%m')
15、查詢距離當(dāng)前現(xiàn)在6個月的數(shù)據(jù)
select * from 表名 where create_date between date_sub(now(),interval 6 month) and now();
16、查詢某一天所在周的第一天
select case when dayname(date('2022-3-10'))='sunday' then date_sub(date('2022-3-10'),interval 6 day) else date_add('2022-3-10',interval -dayofweek(date('2022-3-10'))+2 day) end
17、查詢某一天所在周的最后一天
select case when dayname(date('2022-3-11'))='sunday' then date('2022-3-11') else date_add('2022-3-11',interval 7-dayofweek('2022-3-11')+1 day) end
18、查詢某一天的所在月的第一天
select date_add( date_add(last_day('2022-06-03'),interval 1 day ),interval -1 month );
19、查詢某一天所在月的最后一天
select last_day('2022-03-03'); select date_format(now(),'%y-%m-%d %h:%i:%s');
20、查詢某一天所在月的天數(shù)
select timestampdiff(day,'2023-03-03',(date_add('2017-03-03',interval 1 month)));
附:日期函數(shù)解析
date_format()
格式化日期格式,“date_format(時間戳,時間格式)”
date_sub()
從日期減去指定的時間間隔;函數(shù)形式DATE_SUB(date,INTERVAL expr type),date 參數(shù)是合法的日期表達(dá)式。expr 參數(shù)是您希望添加的時間間隔,時間間隔參數(shù)非常全面,常用的為 年月日時分秒;
//減天數(shù) date_sub(時間,INTERVAL 1 DAY) //減月份 date_sub(時間,INTERVAL 1 MONTH)
yearweek()
返回指定的日期是哪一年的哪個星期,函數(shù)形式為 YEARWEEK(date[,mode]) ,其中date的格式一般為‘年-月-日’,mode為1代表一個星期從星期一開始;
weekday()
接受1個參數(shù),即DATE或DATETIME值,函數(shù)形式為WEEKDAY(date);返回一個整數(shù),范圍從0到6,表示星期一到星期日,即星期一為0,星期二為1,星期日為6;
period_diff()
返回兩個時間相差的月份數(shù),注意它是前一個時間減后一個時間。
注意: period1和period2的格式應(yīng)相同。
SELECT PERIOD_DIFF(201710, 201703);
總結(jié)
到此這篇關(guān)于mysql數(shù)據(jù)庫常見日期查詢方法的文章就介紹到這了,更多相關(guān)mysql日期查詢方法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
mysql 8.0.18.zip安裝配置方法圖文教程(windows 64位)
這篇文章主要為大家詳細(xì)介紹了mysql 8.0.18.zip安裝配置方法圖文教程,以及卸載以前數(shù)據(jù)庫的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-10-10mysql數(shù)據(jù)庫導(dǎo)出xml的實(shí)現(xiàn)方法
因?yàn)橛腥藛柕饺绾螌ysql數(shù)據(jù)庫導(dǎo)出為xml文件,所以發(fā)現(xiàn)了這篇文章2008-09-09mysql導(dǎo)出表的字段和相關(guān)屬性的步驟方法
在本篇文章里小編給大家分享了關(guān)于mysql導(dǎo)出表的字段和相關(guān)屬性的步驟方法,有需要的朋友們跟著學(xué)習(xí)下。2019-01-01