MySql 按周 日 月 年統計

2021-09-11 11:42:01 字數 2210 閱讀 8648

#今天  

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

複製**

date_default_timezone_set('prc'); //預設時區  

echo

"今天:",date("y-m-d",time()),"

";

echo

"今天:",date("y-m-d",strtotime("18 june 2008")),"

";

echo

"昨天:",date("y-m-d",strtotime("-1 day")), "

";

echo

"明天:",date("y-m-d",strtotime("+1 day")), "

";

echo

"一周後:",date("y-m-d",strtotime("+1 week")), "

";

echo

"一周零兩天四小時兩秒後:",date("y-m-d g:h:s",strtotime("+1 week 2 days 4 hours 2 seconds")), "

";

echo

"下個星期四:",date("y-m-d",strtotime("next thursday")), "

";

echo

"上個周一:".date("y-m-d",strtotime("last monday"))."

";

echo

"乙個月前:".date("y-m-d",strtotime("last month"))."

";

echo

"乙個月後:".date("y-m-d",strtotime("+1 month"))."

";

echo

"十年後:".date("y-m-d",strtotime("+10 year"))."

";

複製**

select date_format(create_time,'%y%u') weeks,count(id) count from role group by weeks;

select date_format(create_time,'%y%m%d') days,count(id) count from role group by days;

select date_format(create_time,'%y%m') months,count(id) count from role group by months

複製**

select from_unixtime(create_time,'%y%u') weeks,count(id) count from role group by weeks;

select from_unixtime(create_time,'%y%m%d') days,count(id) count from role group by days;

select from_unixtime(create_time,'%y%m') months,count(id) count from role group by months

複製**

MySQL按小時,日,月,年等條件統計查詢

在地災系統中雨量歷史資料查詢條件有5分鐘,30分鐘,1小時,3小時,6小時,12小時和24小時,原本的查詢語句為 if ylzb.equals 0 else if ylzb.equals 0.5 else假設查詢2018 8 1 2018 8 16這段時間的資料,當ylzb等於0.5或者0時資料能夠...

oracle 當前時間下,周 日 月的交易統計

日 select count 1 from student bill where 1 1 and sub merchant id and to char pay time,yyyy mm dd to char to date yyyy mm dd hh24 mi ss yyyy mm dd and ...

Mysql按年按月按日按周統計查詢

select date format created date,y m d as time,sum money money from o finance detail where org id 1000 group by time select date format created date,y ...