国产99久久精品_欧美日本韩国一区二区_激情小说综合网_欧美一级二级视频_午夜av电影_日本久久精品视频

最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當前位置: 首頁 - 科技 - 知識百科 - 正文

MYSQL每隔10分鐘進行分組統計的實現方法

來源:懂視網 責編:小采 時間:2020-11-09 09:13:38
文檔

MYSQL每隔10分鐘進行分組統計的實現方法

MYSQL每隔10分鐘進行分組統計的實現方法:Mysql關系型數據庫管理系統MySQL是一個開放源碼的小型關聯式數據庫管理系統,開發者為瑞典MySQL AB公司。MySQL被廣泛地應用在Internet上的中小型網站中。由于其體積小、速度快、總體擁有成本低,尤其是開放源碼這一特點,許多中小型網站為了降低網站總體擁有
推薦度:
導讀MYSQL每隔10分鐘進行分組統計的實現方法:Mysql關系型數據庫管理系統MySQL是一個開放源碼的小型關聯式數據庫管理系統,開發者為瑞典MySQL AB公司。MySQL被廣泛地應用在Internet上的中小型網站中。由于其體積小、速度快、總體擁有成本低,尤其是開放源碼這一特點,許多中小型網站為了降低網站總體擁有

Mysql關系型數據庫管理系統

MySQL是一個開放源碼的小型關聯式數據庫管理系統,開發者為瑞典MySQL AB公司。MySQL被廣泛地應用在Internet上的中小型網站中。由于其體積小、速度快、總體擁有成本低,尤其是開放源碼這一特點,許多中小型網站為了降低網站總體擁有成本而選擇了MySQL作為網站數據庫。

這篇文章主要給大家介紹了如何利用MYSQL實現每隔10分鐘進行分組統計的方法,文中給出了詳細的示例代碼,相信對大家的理解和學習具有一定的參考借鑒價值,有需要的朋友們下面來一起看看吧。

前言

本文的內容主要是介紹了MYSQL每隔10分鐘進行分組統計的實現方法,在畫用戶登錄、操作情況在一天內的分布圖時會非常有用,之前我只知道用「存儲過程」實現的方法(雖然執行速度快,但真的是太不靈活了),后來學會了用高級點的「group by」方法來靈活實現類似功能。

正文:

-- time_str '2016-11-20 04:31:11'
-- date_str 20161120

select concat(left(date_format(time_str, '%y-%m-%d %h:%i'),15),'0') as time_flag, count(*) as count from `security`.`cmd_info` where `date_str`=20161120 
group by time_flag order by time_flag; -- 127 rows

select round(unix_timestamp(time_str)/(10 * 60)) as timekey, count(*) from `security`.`cmd_info` where `date_str`=20161120 group by timekey 
order by timekey; -- 126 rows

-- 以上2個SQL語句的思路類似——使用「group by」進行區分,但是方法有所不同,前者只能針對10分鐘(或1小時)級別,后者可以動態調整間隔大小,兩者效率差不多,
可以根據實際情況選用

select concat(date(time_str),' ',hour(time_str),':',round(minute(time_str)/10,0)*10), count(*) from `security`.`cmd_info` where `date_str`=20161120 
group by date(time_str), hour(time_str), round(minute(time_str)/10,0)*10; -- 145 rows

select concat(date(time_str),' ',hour(time_str),':',floor(minute(time_str)/10)*10), count(*) from `security`.`cmd_info` where `date_str`=20161120 
group by date(time_str), hour(time_str), floor(minute(time_str)/10)*10; -- 127 rows (和 date_format 那個等價)

select concat(date(time_str),' ',hour(time_str),':',ceil(minute(time_str)/10)*10), count(*) from `security`.`cmd_info` where `date_str`=20161120 
group by date(time_str), hour(time_str), ceil(minute(time_str)/10)*10; -- 151 rows

&

DELIMITER //

DROP PROCEDURE IF EXISTS `usp_cmd_info`;

CREATE PROCEDURE `usp_cmd_info`(IN dates VARCHAR(12))
BEGIN
 SELECT count(*) from `cmd_info` where `time_str` BETWEEN CONCAT(dates, " 00:00:00") AND CONCAT(dates, " 00:10:00") 
 INTO @count_0;
 SELECT count(*) from `cmd_info` where `time_str` BETWEEN CONCAT(dates, " 00:10:00") AND CONCAT(dates, " 00:20:00") 
 INTO @count_1;
 ...
 SELECT count(*) from `cmd_info` where `time_str` BETWEEN CONCAT(dates, " 23:40:00") AND CONCAT(dates, " 23:50:00") 
 INTO @count_142;
 SELECT count(*) from `cmd_info` where `time_str` BETWEEN CONCAT(dates, " 23:50:00") AND CONCAT(dates, " 23:59:59") 
 INTO @count_143;
 select @count_0, @count_1, @count_2, @count_3, @count_4, @count_5, @count_6, @count_7, @count_8, @count_9, @count_10, 
 @count_11, @count_12, @count_13, @count_14, @count_15, @count_16, @count_17, @count_18, @count_19, @count_20, @count_21, 
 @count_22, @count_23, @count_24, @count_25, @count_26, @count_27, @count_28, @count_29, @count_30, @count_31, @count_32, 
 @count_33, @count_34, @count_35, @count_36, @count_37, @count_38, @count_39, @count_40, @count_41, @count_42, @count_43, 
 @count_44, @count_45, @count_46, @count_47, @count_48, @count_49, @count_50, @count_51, @count_52, @count_53, @count_54, 
 @count_55, @count_56, @count_57, @count_58, @count_59, @count_60, @count_61, @count_62, @count_63, @count_64, @count_65, 
 @count_66, @count_67, @count_68, @count_69, @count_70, @count_71, @count_72, @count_73, @count_74, @count_75, @count_76, 
 @count_77, @count_78, @count_79, @count_80, @count_81, @count_82, @count_83, @count_84, @count_85, @count_86, @count_87,
 @count_88, @count_89, @count_90, @count_91, @count_92, @count_93, @count_94, @count_95, @count_96, @count_97, @count_98, 
 @count_99, @count_100, @count_101, @count_102, @count_103, @count_104, @count_105, @count_106, @count_107, @count_108, 
 @count_109, @count_110, @count_111, @count_112, @count_113, @count_114, @count_115, @count_116, @count_117, @count_118, 
 @count_119, @count_120, @count_121, @count_122, @count_123, @count_124, @count_125, @count_126, @count_127, @count_128, 
 @count_129, @count_130, @count_131, @count_132, @count_133, @count_134, @count_135, @count_136, @count_137, @count_138, 
 @count_139, @count_140, @count_141, @count_142, @count_143;
END //

DELIMITER ;

show PROCEDURE status\G

CALL usp_cmd_info("2016-10-20");
上面的這段MySQL存儲過程的語句非常長,不可能用手工輸入,可以用下面的這段Python代碼按所需的時間間隔自動生成:
import datetime

today = datetime.date.today()
# 或 由給定格式字符串轉換成
# today = datetime.datetime.strptime('2016-11-21', '%Y-%m-%d')

min_today_time = datetime.datetime.combine(today, datetime.time.min) # 2016-11-21 00:00:00
max_today_time = datetime.datetime.combine(today, datetime.time.max) # 2016-11-21 23:59:59

sql_procedure_arr = []
sql_procedure_arr2 = []
for x in xrange(0, 60*24/5, 1):
 start_datetime = min_today_time + datetime.timedelta(minutes = 5*x)
 end_datetime = min_today_time + datetime.timedelta(minutes = 5*(x+1))
 # print x, start_datetime.strftime("%Y-%m-%d %H:%M:%S"), end_datetime.strftime("%Y-%m-%d %H:%M:%S")
 select_str = 'SELECT count(*) from `cmd_info` where `time_str` BETWEEN "{0}" AND "{1}" INTO @count_{2};'.format(start_datetime, end_datetime, x)
 # print select_str
 sql_procedure_arr.append(select_str)
 sql_procedure_arr2.append('@count_{0}'.format(x))
print '\n'.join(sql_procedure_arr)
print 'select {0};'.format(', '.join(sql_procedure_arr2))

總結

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

MYSQL每隔10分鐘進行分組統計的實現方法

MYSQL每隔10分鐘進行分組統計的實現方法:Mysql關系型數據庫管理系統MySQL是一個開放源碼的小型關聯式數據庫管理系統,開發者為瑞典MySQL AB公司。MySQL被廣泛地應用在Internet上的中小型網站中。由于其體積小、速度快、總體擁有成本低,尤其是開放源碼這一特點,許多中小型網站為了降低網站總體擁有
推薦度:
標簽: 分組 10 實現
  • 熱門焦點

最新推薦

猜你喜歡

熱門推薦

專題
Top
主站蜘蛛池模板: 久久国产精品久久久久久久久久 | 国产成人高清亚洲一区91 | 国产精品亚洲一区二区三区在线播放 | 色妞综合 | 国产成人精品.一二区 | 欧美日本激情 | 丝袜制服中文字幕 | 日本欧美另类 | 国产成人精品在线 | 欧美精品一区二区三区免费播放 | 国产精品高清一区二区三区 | 亚洲国产一区二区三区在线观看 | 日韩亚洲欧洲在线com91tv | 特黄日韩免费一区二区三区 | 欧美日韩伦理 | 91色老99久久九九爱精品 | 欧美极品第一页 | 欧美精品在线免费 | 国产成人一区二区三区免费观看 | 日韩欧美三级视频 | 国产精品久久二区三区色裕 | 欧美日韩视频在线播放 | 最新偷窥盗摄视频在线 | xx日韩| 国产三级网站在线观看 | 精品久久中文网址 | 欧美啊v在线观看 | 日韩欧美亚洲综合一区二区 | 久久久高清国产999尤物 | 久久精品一区 | 国产日韩欧美一区二区三区视频 | 国产精品高清久久久久久久 | 亚洲页码 | 一区二区三区高清不卡 | 一级免费毛片 | 亚洲最新 | 国产欧美综合在线观看第七页 | 欧美国产日韩一区二区三区 | 国产aⅴ一区二区三区 | 日韩欧美国产精品第一页不卡 | 亚洲欧洲高清有无 |