MYSQL如何统计每个时段(比如1天24个时段)的注册人数

ganwu222 2010-08-26 04:05:53
有个time字段
想做个统计
统计每个时段的注册人数
比如统计今天24个时段之内的每个时段注册人数
意思是这样:
00:00 - 01:00
01:00 - 02:00
不知道SQL语句如何写
谢谢指点
...全文
1997 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
ganwu222 2010-08-27
  • 打赏
  • 举报
回复
谢谢
FROM_UNIXTIME
又学了一个
xuzuning 2010-08-27
  • 打赏
  • 举报
回复 1
手快了,不好意思
要这样写
select FROM_UNIXTIME(`atvtime`, '%Y%m%d%H') as hh, count(*) as cnt from bao_players_info group by hh
xuzuning 2010-08-27
  • 打赏
  • 举报
回复
你的 atvtime 字段是数值型的吗?
保存的是 unix 时间戳?
怎么不早说?
ganwu222 2010-08-27
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 amani11 的回复:]
select HOUR(time) as hh, count(*) as cnt from tbl_name group by hh

select count(*) from table_name where time > startime and time < endtime

你把这两句结合一下不就可以了?

select HOUR(time) as hh, count(*) a……
[/Quote]

这个也不行呢
select HOUR(`atvtime`) as hh,count(*) as cnt from bao_players_info WHERE `atvtime` >= 1282838400 AND `atvtime` <1283011199 group by hh

得到这个数据
hh cnt
NULL 63
838 30

allniu 2010-08-26
  • 打赏
  • 举报
回复
使用MYSQL函数 substring 与 group by 实现, 此为正解,
欢迎到 http://www.allniu.com/list.php?catid=33 去看看,好多MYSQL知识
xuzuning 2010-08-26
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 ganwu222 的回复:]
我自己写的 你们看看 好长啊

SQL code
SELECT
CASE
WHEN `atvtime` >1282665600
AND `atvtime` <=1282669200
THEN '1'
WHEN `atvtime` >1282669200
AND `atvtime` <=1282672800
THEN '2'
WHEN `atvtime` >12826728……
[/Quote]
你在开玩笑吧?这只是今天,第二天就没用了
amani11 2010-08-26
  • 打赏
  • 举报
回复
select HOUR(time) as hh, count(*) as cnt from tbl_name group by hh

select count(*) from table_name where time > startime and time < endtime

你把这两句结合一下不就可以了?

select HOUR(time) as hh, count(*) as cnt from tbl_name WHERE time > startime and time < endtime group by hh
ganwu222 2010-08-26
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 cunningboy 的回复:]
写出来就好了,反正也才24个情况而已,又不是几百个
[/Quote]
呵呵 主要是不知道有没有简单的方法,我写的这个太死了
CunningBoy 2010-08-26
  • 打赏
  • 举报
回复
写出来就好了,反正也才24个情况而已,又不是几百个
ganwu222 2010-08-26
  • 打赏
  • 举报
回复
我自己写的 你们看看 好长啊
SELECT 
CASE
WHEN `atvtime` >1282665600
AND `atvtime` <=1282669200
THEN '1'
WHEN `atvtime` >1282669200
AND `atvtime` <=1282672800
THEN '2'
WHEN `atvtime` >1282672800
AND `atvtime` <=1282676400
THEN '3'
WHEN `atvtime` >1282676400
AND `atvtime` <=1282680000
THEN '4'
WHEN `atvtime` >1282680000
AND `atvtime` <=1282683600
THEN '5'
WHEN `atvtime` >1282683600
AND `atvtime` <=1282687200
THEN '6'
WHEN `atvtime` >1282687200
AND `atvtime` <=1282690800
THEN '7'
WHEN `atvtime` >1282690800
AND `atvtime` <=1282694400
THEN '8'
WHEN `atvtime` >1282694400
AND `atvtime` <=1282698000
THEN '9'
WHEN `atvtime` >1282698000
AND `atvtime` <=1282701600
THEN '10'
WHEN `atvtime` >1282701600
AND `atvtime` <=1282705200
THEN '11'
WHEN `atvtime` >1282705200
AND `atvtime` <=1282708800
THEN '12'
WHEN `atvtime` >1282708800
AND `atvtime` <=1282712400
THEN '13'
WHEN `atvtime` >1282712400
AND `atvtime` <=1282716000
THEN '14'
WHEN `atvtime` >1282716000
AND `atvtime` <=1282719600
THEN '15'
WHEN `atvtime` >1282719600
AND `atvtime` <=1282723200
THEN '16'
WHEN `atvtime` >1282723200
AND `atvtime` <=1282726800
THEN '17'
WHEN `atvtime` >1282726800
AND `atvtime` <=1282730400
THEN '18'
WHEN `atvtime` >1282730400
AND `atvtime` <=1282734000
THEN '19'
WHEN `atvtime` >1282734000
AND `atvtime` <=1282737600
THEN '20'
WHEN `atvtime` >1282737600
AND `atvtime` <=1282741200
THEN '21'
WHEN `atvtime` >1282741200
AND `atvtime` <=1282744800
THEN '22'
WHEN `atvtime` >1282744800
AND `atvtime` <=1282748400
THEN '23'
WHEN `atvtime` >1282748400
AND `atvtime` <=1282751999
THEN '24'
END ct2, count( * ) AS ct
FROM bao_players_info
WHERE `atvtime` >=1282665600
AND `atvtime` <=1282751999
AND `atvgamestype` =11
GROUP BY
CASE
WHEN `atvtime` >1282665600
AND `atvtime` <=1282669200
THEN '1'
WHEN `atvtime` >1282669200
AND `atvtime` <=1282672800
THEN '2'
WHEN `atvtime` >1282672800
AND `atvtime` <=1282676400
THEN '3'
WHEN `atvtime` >1282676400
AND `atvtime` <=1282680000
THEN '4'
WHEN `atvtime` >1282680000
AND `atvtime` <=1282683600
THEN '5'
WHEN `atvtime` >1282683600
AND `atvtime` <=1282687200
THEN '6'
WHEN `atvtime` >1282687200
AND `atvtime` <=1282690800
THEN '7'
WHEN `atvtime` >1282690800
AND `atvtime` <=1282694400
THEN '8'
WHEN `atvtime` >1282694400
AND `atvtime` <=1282698000
THEN '9'
WHEN `atvtime` >1282698000
AND `atvtime` <=1282701600
THEN '10'
WHEN `atvtime` >1282701600
AND `atvtime` <=1282705200
THEN '11'
WHEN `atvtime` >1282705200
AND `atvtime` <=1282708800
THEN '12'
WHEN `atvtime` >1282708800
AND `atvtime` <=1282712400
THEN '13'
WHEN `atvtime` >1282712400
AND `atvtime` <=1282716000
THEN '14'
WHEN `atvtime` >1282716000
AND `atvtime` <=1282719600
THEN '15'
WHEN `atvtime` >1282719600
AND `atvtime` <=1282723200
THEN '16'
WHEN `atvtime` >1282723200
AND `atvtime` <=1282726800
THEN '17'
WHEN `atvtime` >1282726800
AND `atvtime` <=1282730400
THEN '18'
WHEN `atvtime` >1282730400
AND `atvtime` <=1282734000
THEN '19'
WHEN `atvtime` >1282734000
AND `atvtime` <=1282737600
THEN '20'
WHEN `atvtime` >1282737600
AND `atvtime` <=1282741200
THEN '21'
WHEN `atvtime` >1282741200
AND `atvtime` <=1282744800
THEN '22'
WHEN `atvtime` >1282744800
AND `atvtime` <=1282748400
THEN '23'
WHEN `atvtime` >1282748400
AND `atvtime` <=1282751999
THEN '24'
END
LIMIT 0 , 30
amani11 2010-08-26
  • 打赏
  • 举报
回复
结合起来

限定一天的时间内,,,结果按照小时group
jlzan1314 2010-08-26
  • 打赏
  • 举报
回复
3楼的不行嘛?
ganwu222 2010-08-26
  • 打赏
  • 举报
回复
还有人吗
ganwu222 2010-08-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 cunningboy 的回复:]
SQL code
select count(*) from table_name where time > startime and time < endtime
[/Quote]
谢谢
但这个要手动输入时段,不能自动统计
xuzuning 2010-08-26
  • 打赏
  • 举报
回复
select HOUR(time) as hh, count(*) as cnt from tbl_name group by hh
wzwen 2010-08-26
  • 打赏
  • 举报
回复
楼上对的
CunningBoy 2010-08-26
  • 打赏
  • 举报
回复
select count(*) from table_name where time > startime and time < endtime

21,891

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧