MySQL日期查詢 本週 本月 本季 本年

2021-07-14 05:09:23 字數 1980 閱讀 3712

查詢當天

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

查詢昨天

select * from 表名 where to_days( now( ) ) – to_days( 時間欄位名) <= 1

查詢一周

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 weekofyear(create_date)=weekofyear(now());

查詢上週

select * from ht_invoice_information where create_date>=date_add(now(),interval -(8 + weekday(now())) day) and create_date<=date_add(now(),interval -(1 + weekday(now())) day); 或者

select * from `ht_invoice_information` where weekofyear(create_date)=weekofyear(date_sub(now(),interval 1 week));

查詢本月

select * from ht_invoice_information where month(create_date)=month(now()) and year(create_date)=year(now());

查詢上月

select * from ht_invoice_information where create_date<=last_day(date_add(now(),interval -1 month)) and create_date>=date_format(concat(extract(year_month from date_add(now(),interval -1 month)),』01′),』%y-%m-%d』); 或者

select * from `ht_invoice_information` where month(create_date)=month(date_sub(now(),interval 1 month))

and year(create_date)=year(now());

查詢本季度

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));

MySQL日期查詢 本週 本月 本季 本年

查詢當天 select from 表名 where to days 時間欄位名 to days now 查詢昨天 select from 表名 where to days now to days 時間欄位名 1 查詢一周 select from 表名 where date sub curdate i...

MySql查詢當天 本週 本月 本季度 本年的資料

select from 表名 where to days 時間欄位名 to days now select from 表名 where to days now to days 時間欄位名 1 select from 表名 where yearweek date format 時間欄位名,y m d ...

MySql查詢當天 本週 本月 本季度 本年的資料

一 建立測試資料 二 sql查詢及示例 1.今天 select from 表名 where to days 時間欄位名 to days now select from demo s where to days s.create time to days now 2.昨天 select from 表名...