Mysql中查詢某一天,某個月,某一年資料

2021-10-07 10:30:32 字數 2992 閱讀 8173

今天

select * from 表名 where to_days(時間欄位名) = to_days(now());
昨天

select * from 表名 where to_days( now( ) ) - to_days( 時間欄位名) <= 1
近7天

select * from 表名 where date_sub(curdate(), interval 7 day) <= date(時間欄位名)
近30天

select * from 表名 where date_sub(curdate(), interval 30 day) <= date(時間欄位名)
本月

select * from 表名 where date_format( 時間欄位名, '%y%m' ) = date_format( curdate( ) , '%y%m' )
上一月

select * from 表名 where period_diff( date_format( now( ) , '%y%m' ) , date_format( 時間欄位名, '%y%m' ) ) =1
查詢本季度資料

select * from `ht_invoice_information` where quarter(create_date)=quarter(now());
查詢上季度資料

select * from `ht_invoice_information` where quarter(create_date)=quarter(date_sub(now(),interval 1 quarter));
查詢本年資料

select * from `ht_invoice_information` where year(create_date)=year(now());
查詢上年資料

select * from `ht_invoice_information` where year(create_date)=year(date_sub(now(),interval 1 year));
查詢當前這週的資料

select name,submittime from enterprise where yearweek(date_format(submittime,'%y-%m-%d')) = yearweek(now());
查詢上週的資料

select name,submittime from enterprise where yearweek(date_format(submittime,'%y-%m-%d')) = yearweek(now())-1;
查詢上個月的資料

select name,submittime from enterprise where date_format(submittime,'%y-%m')=date_format(date_sub(curdate(), interval 1 month),'%y-%m')

select * from user where date_format(pudate,'%y%m') = date_format(curdate(),'%y%m') ;

select * from user where weekofyear(from_unixtime(pudate,'%y-%m-%d')) = weekofyear(now())

select * from user where month(from_unixtime(pudate,'%y-%m-%d')) = month(now())

select * from user where year(from_unixtime(pudate,'%y-%m-%d')) = year(now()) and month(from_unixtime(pudate,'%y-%m-%d')) = month(now())

select * from user where pudate between 上月最後一天 and 下月第一天

查詢當前月份或任意某個月的資料

select name,submittime from enterprise where date_format(submittime,'%y-%m')=date_format(now(),'%y-%m') 

//=後面可換為具體時間,查詢任意月份的資料

查詢距離當前時間6個月的資料

select name,submittime from enterprise where submittime between date_sub(now(),interval 6 month) and now();
查詢某個月的資料(查詢17年10月份資料)

select * from exam where date_format(starttime,'%y-%m')='2017-10'
select * from exam where date_format(starttime,'%y-%m')=date_format('2017-10-05','%y-%m')

mysql查詢某一天或某段時間的資料

今天 select from 表名 where to days 時間欄位名 to days now 昨天 select from 表名 where to days now to days 時間欄位名 1近7天 select from 表名 where date sub curdate interva...

mysql 查詢某一年 某一月 某一天的資料

查詢某一年 某一月 某一天的資料 可組合 select fromymt where date format indate,y m d 2016 10 10 indate為表中列名 mysql查詢一天,查詢一周,查詢乙個月內的資料 查詢一天 select from ymt where date ind...

Mysql 按時間段 某一天 查詢

今天 select from 表名 where to days 時間欄位名 to days now 昨天 select from 表名 where datediff 字段,now 1 本週 select name,submittime from enterprise where yearweek d...