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

2021-09-26 12:53:58 字數 1167 閱讀 3075

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

查詢在某段日期之間的資料:

select * from 資料表  where 時間欄位名 between '2016-02-01' and '2016-02-05'

查詢往前3個月的資料:

select * from 資料表 where 時間欄位名 between date_sub(now(),interval 3 month) and now()

查詢往前一年的資料:

select * from 資料表  where 時間欄位名 between date_sub(now(),interval 1 year) and now()

查詢本月的資料:

select * from 資料表 where date_format(時間欄位名,'%y-%m')=date_format(now(),'%y-%m')

查詢上月的資料:

select * from 資料表 where date_format(時間欄位名,'%y-%m')=date_format(date_sub(curdate(), interval 1 month),'%y-%m')

查詢本週的資料:

select * from 資料表 where yearweek(date_format(時間欄位名,'%y-%m-%d')) = yearweek(now())

查詢上週資料:

select * from 資料表 where yearweek(date_format(create_time,'%y-%m-%d')) = yearweek(now())-1

查詢往前7天的資料:

select * from 資料表 where date_sub(curdate(), interval 7 day) <= 你要判斷的時間欄位名

查詢往前30天的資料:

select * from 資料表 where date_sub(curdate(), interval 30 day) <= 你要判斷的時間欄位名

根據國人習慣 周一為一周的第一天習慣算一周的資料

select * from t_browse_statistics where yearweek(date_format(brows_date,'%y-%m-%d'),1) = yearweek(now())+1

order by brows_date desc

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...

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

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