查詢時間範圍與YEARWEEK用法

2021-10-13 21:39:17 字數 1610 閱讀 9050

目錄

1、根據時間範圍查詢語句

2、yearweek

//今天

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;

yearweek(date[,mode]) ,mode為0表示一周從週日開始,為1表示一周從周一開始,如下示例(2021-1-10是21年第2周的週日)

(1)mode為0時

(2)(1)mode為空時,預設從週日開始,同0

(3)mode為1時,預設從周一開始

mysql查詢時間範圍

前端傳入的引數確是 yyyy mm dd 的 沒有帶時分秒。如果按照下面這兩種方式會查不全 select from test where create time between 2018 07 30 and 2018 07 31 select from test where create time ...

Mysql查詢時間範圍

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

mysql 查詢時間轉換 Mysql 查詢時間轉換

利用data format函式 select date format now y m d 根據format字串格式化date值 s,s 兩位數字形式的秒 00,01,59 i,i 兩位數字形式的分 00,01,59 h 兩位數字形式的小時,24 小時 00,01,23 h 兩位數字形式的小時,12 ...