mysql 分组统计多少个数据

mysql 分组统计多少个数据,第1张

-- 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 sum(ifnull(t2.total,0)), t1.flag

from B t1

left join A t2 on t1.id=t2.flag

group by t1.flag

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 那个等价)


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/7624090.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-07
下一篇 2023-04-07

发表评论

登录后才能评论

评论列表(0条)

保存