Mysql 用於統計的時間函式用法

2021-09-25 21:25:27 字數 2856 閱讀 2695

今天

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 `表名` where quarter(create_date)=quarter(now());
查詢上季度資料

select * from `表名` where quarter(create_date)=quarter(date_sub(now(),interval 1 quarter));
查詢本年資料

select * from `表名` where year(create_date)=year(now());
查詢上年資料

select * from `表名` where year(create_date)=year(date_sub(now(),interval 1 year));
查詢當前這週的資料

select name,submittime from `表名` where yearweek(date_format(submittime,'%y-%m-%d')) = yearweek(now());
查詢上週的資料

select name,submittime from `表名` 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')
查詢距離當前現在6個月的資料

select name,submittime from enterprise where submittime between date_sub(now(),interval 6 month) and now();
查詢某個月的資料(查詢17年10月份資料)

select * from exam   where date_format(starttime,'%y-%m')='2017-10'
select * from exam   where date_format(starttime,'%y-%m')=date_format('2017-10-05','%y-%m')

MySQL統計函式記錄 時間段統計

按年彙總,統計 select sum mymoney as totalmoney,count as sheets from mytable group by date format col,y 按月彙總,統計 select sum mymoney as totalmoney,count as she...

delphi用於時間操作的函式 2

tdatetime型別與字串型轉換函式 datetimetostr 函式 datetostr 函式 timetostr函式 定義 function datetimetostr datetime tdatetime string function timetostr time tdatetime st...

Mysql按照時間分組統計

select update time,count id as count from eemp track group by substring update time,1,10 select date format create time,y m d as days count as count f...