MYSQL 常用日期統計

2021-09-08 07:16:19 字數 2138 閱讀 7338

//今天

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 * fromht_invoice_informationwhere quarter(create_date)=quarter(now());

//查詢上季度資料

select * fromht_invoice_informationwhere quarter(create_date)=quarter(date_sub(now(),interval 1 quarter));

//查詢本年資料

select * fromht_invoice_informationwhere year(create_date)=year(now());

//查詢上年資料

select * fromht_invoice_informationwhere 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』)

參考**:

mysql 按日期 Mysql 中按日期統計資料

select date format create time,y u weeks,count caseid count from tc case group by weeks select date format create time,y m d days,count caseid count f...

MySql按日期進行統計

當天的資料 select from 表 where date 時間欄位名 curdate 當月的資料 select from 表 where date format 時間欄位名,y m date format curdate y m 昨天 select from 表名 where to days n...

mysql 日期常用函式

mysql 中最重要的內建日期函式 函式描述 now 返回當前的日期和時間 curdate 返回當前的日期 curtime 返回當前的時間 date 提取日期或日期 時間表示式的日期部分 extract 返回日期 時間按的單獨部分 date add 給日期新增指定的時間間隔 date sub 從日期...