根據最近日期查詢的sql

2021-08-21 08:27:32 字數 581 閱讀 5687

幾個小時內的資料

date_sub(now(), interval 5 hour)

今天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

根據日期查詢的一些sql

文章 發布於2020年7月14日 根據日期查詢的一些sql 查詢資料 查詢上個月的資料 select fromcount referralwhere period diff date format now y m date format update time,y m 1 查詢本月資料 select...

MySQL關於根據日期查詢資料的sql語句

查詢往前7天的資料 select from 資料表 where date sub curdate interval 7 day 你要判斷的時間欄位名 查詢往前30天的資料 select from 資料表 where date sub curdate interval 30 day 你要判斷的時間欄位...

MySQL關於根據日期查詢資料的sql語句

查詢在某段日期之間的資料 select from 資料表 where 時間欄位名 between 2016 02 01 and 2016 02 05 查詢往前3個月的資料 select from 資料表 where 時間欄位名 between date sub now interval 3 mont...