234567891011121314 -- 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 那个等价)
时间段统计,可以采用 hour(subscribe_time) 取出小时然后分层。思路:
select
uid
,CASE WHEN HOUR(subscribe_time) BETWEEN 0 AND 1 THEN '00:00:00'
WHEN HOUR(subscribe_time) BETWEEN 2 AND 3 THEN '02:00:00'
...
ELSE '23:00:00' END -- 生成时间分层部分,insert前外层sql加上日期后作为唯一的时间值
,COUNT(*)
FROM yht_fans WHERE subscribe_time>=UNIX_TIMESTAMP(CURDATE()) and subscribe_time<=UNIX_TIMESTAMP(CURDATE())+86400
GROUP BY 1,2
评论列表(0条)