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

2021-12-30 08:24:59 字數 3354 閱讀 1566

在地災系統中雨量歷史資料查詢條件有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時資料能夠正常返回,但等於其他值時只會返回第一天的資料,時間間隔為ylzb。

為解決此問題查詢mysql按照n分鐘,n小時,n天,n年等查詢問題。

問題原因是最後乙個查詢語句group by裡面缺少年月日。

當時間間隔單位為1時。可直接用如下語句查詢:

//按1小時查詢

select sum(sensordata1) ,date_format(sendtime,'%y-%m-%d %h:%i') from `data0028-gx-1a04001` e

where sendtime>='2018-08-01 00:00:00' and sendtime <= '2018-08-16 23:59:59'

group by date_format(sendtime,'%y-%m-%d %h') ;

//按1天查詢

select sum(sensordata1) ,date_format(sendtime,'%y-%m-%d %h:%i') from `data0028-gx-1a04001` e

where sendtime>='2018-08-01 00:00:00' and sendtime <= '2018-08-16 23:59:59'

group by date_format(sendtime,'%y-%m-%d') ;

//按1月查詢

select sum(sensordata1) ,date_format(sendtime,'%y-%m-%d %h:%i') from `data0028-gx-1a04001` e

where sendtime>='2018-08-01 00:00:00' and sendtime <= '2018-08-16 23:59:59'

group by date_format(sendtime,'%y-%m') ;

//按1年查詢

select sum(sensordata1) ,date_format(sendtime,'%y-%m-%d %h:%i') from `data0028-gx-1a04001` e

where sendtime>='2018-08-01 00:00:00' and sendtime <= '2018-08-16 23:59:59'

group by date_format(sendtime,'%y') ;

當時間間隔單位不為1,比如6小時,此刻可以用div或者floor作為時間分割,**如下:

select cast(sum(sensordata1) as decimal(19,2)) ,date_format(sendtime,'%y-%m-%d %h:%i')

from `data0028-gx-1a04001` e where sendtime>='2018-08-01 00:00:00'

and sendtime <= '2018-08-16 23:59:59'

group by date_format(sendtime,'%y-%m-%d') ,hour(e.sendtime) div 6 ;

或者select cast(sum(sensordata1) as decimal(19,2)) ,date_format(sendtime,'%y-%m-%d %h:%i')

from `data0028-gx-1a04001` e where sendtime>='2018-08-01 00:00:00'

and sendtime <= '2018-08-16 23:59:59'

group by date_format(sendtime,'%y-%m-%d') ,floor(hour(e.sendtime)/6);

搜尋結果為(兩個查詢語句的結果一樣):

這裡需要注意兩個問題:

cast和decimal

1.cast在這裡的作用為使得sensordata1可以轉換為固定的浮點格式增加,以免結果小數字數發生錯誤

2.decimal是設定格式的作用,decimal(a,b)中a表示有效數字的精度,即這個數字一共有多少位;b表示小數點的位數。

如果不這樣設定,而是直接寫sum(sensordata1),有的結果會有很多的小數字。如圖所示:

年月日表達格式

%s, %s 兩位數字形式的秒( 00,01, …, 59)

%i 兩位數字形式的分( 00,01, …, 59)

%h 兩位數字形式的小時,24 小時(00,01, …, 23)

%h, %i 兩位數字形式的小時,12 小時(01,02, …, 12)

%k 數字形式的小時,24 小時(0,1, …, 23)

%l 數字形式的小時,12 小時(1, 2, …, 12)

%t 24 小時的時間形式(hh : mm : s s)

%r 12 小時的時間形式(hh:mm:ss am 或hh:mm:ss pm)

%p am 或p m

%w 一周中每一天的名稱( sunday, monday, …, saturday)

%a 一周中每一天名稱的縮寫( sun, mon, …, sat)

%d 兩位數字表示月中的天數( 00, 01, …, 31)

%e 數字形式表示月中的天數( 1, 2, …, 31)

%d 英文本尾表示月中的天數( 1st, 2nd, 3rd, …)

%w 以數字形式表示週中的天數( 0 = sunday, 1=monday, …, 6=saturday)

%j 以三位數字表示年中的天數( 001, 002, …, 366)

% u 周(0, 1, 52),其中sunday 為週中的第一天

%u 周(0, 1, 52),其中monday 為週中的第一天

%m 月名(january, february, …, december)

%b 縮寫的月名( january, february, …, december)

%m 兩位數字表示的月份( 01, 02, …, 12)

%c 數字表示的月份( 1, 2, …, 12)

%y 四位數字表示的年份

%y 兩位數字表示的年份

%% 直接值「%」

MySql 按周 日 月 年統計

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

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 months,count caseid co...

MySql按分鐘,小時,天,月,年進行統計查詢

很多日誌統計的情況中,會出現按照小時,天,月來進行統計分析 以分鐘為單位做日誌統計分析 select count as n,date format inserttime,y m d h i 00 from data log 201904 u where inserttime 2019 04 01 0...