MySql按日期進行統計

2021-10-08 21:19:23 字數 1704 閱讀 7016

當天的資料

select * from 表 where date( 時間欄位名 ) = curdate();
當月的資料

select *from 表 where date_format( 時間欄位名,'%y%m')=date_format(curdate( ),'%y%m')
昨天

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;
查詢當前月份的資料

select name,submittime from enterprise   where date_format(submittime,'%y-%m')=date_format(now(),'%y-%m')
查詢距離當前現在6個月的資料

select name,submittime from enterprise where submittime between date_sub(now(),interval 6 month) and now();

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 date format create time,y m d sum user id from orders where order state 2 group by date format create time,y m...

MySQL按周進行消費排行統計

表dish 欄位dish id,dish name,create time 表select food 欄位dish id,amount,create time 問題 輸入乙個date,獲取當周的菜品消費數量排行 最重要的部分 如何按周進行統計?答案或許有很多種,我用的是這一種 date format...