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)文章
LEFT JOIN條件在on后面和在where后面的區(qū)別及說明
這篇文章主要介紹了LEFT JOIN條件在on后面和在where后面的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09
MySQL數(shù)據(jù)庫定時備份的幾種實現(xiàn)方法
本文主要介紹了MySQL數(shù)據(jù)庫定時備份的幾種實現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07
MySQL中刪除重復(fù)數(shù)據(jù)的簡單方法
這篇文章主要介紹了MySQL中刪除重復(fù)數(shù)據(jù)的簡單方法,比起一般的NOT IN語句的效率更為高,需要的朋友可以參考下2015-05-05
mysql報錯:1406 Data too long for colu
這篇文章給大家介紹了多種解決mysql報錯:1406, Data too long for column的解決方法,如果有遇到相同問題的朋友可以參考閱讀本文,對解決問題有一定的幫助,需要的朋友可以參考下2023-09-09
MYSQL查詢結(jié)果實現(xiàn)發(fā)送給客戶端
這篇文章主要介紹了MYSQL查詢結(jié)果實現(xiàn)發(fā)送給客戶端方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-06-06

