Mysql 从visitlogs表中计算每小时的访问量
我有一张表格,记录了一些地点的访问信息以及时间,可能还有一些关于这个地方的评论,如:Mysql 从visitlogs表中计算每小时的访问量,mysql,Mysql,我有一张表格,记录了一些地点的访问信息以及时间,可能还有一些关于这个地方的评论,如: +---------+---------+------------+-----------------------+---------------------+ | visitId | userId | locationId | comments | time | +---------+---------+------------+-----------
+---------+---------+------------+-----------------------+---------------------+
| visitId | userId | locationId | comments | time |
+---------+---------+------------+-----------------------+---------------------+
| 1 | 3 | 12 | It's a good day here! | 2012-12-12 20:50:12 |
+---------+---------+------------+-----------------------+---------------------+
我想做的是按小时计算访问组的数量,我想这样生成结果:
+------------+-----+-----+-----+-----+-----+-----+-------+------+
| locationId | 0 | 1 | 2 | 3 | 4 | 5 | ... | 23 |
+------------+-----+-----+-----+-----+-----+-----+-------+------+
| 12 | 15 | 12 | 34 | 67 | 78 | 89 | ... | 34 |
+------------+-----+-----+-----+-----+-----+-----+-------+------+
我该怎么做?
我想评估一整天的访问量差异。这将为您提供每个地点ID每小时的访问量
SELECT locationId, HOUR(time), COUNT(*)
FROM table
GROUP BY locationId, HOUR(time)
这将为您提供每个locationid每小时的访问次数
SELECT locationId, HOUR(time), COUNT(*)
FROM table
GROUP BY locationId, HOUR(time)
如果不进行测试,我认为这将起作用:
select locationid, hour(time) as hour, count(distinct userid) as usercnt
from table_1
group by locationid, hour;
它不会像你建议的那样被转换,但是数据是相同的,没有测试,我认为这会起作用:
select locationid, hour(time) as hour, count(distinct userid) as usercnt
from table_1
group by locationid, hour;
它不会按照您的建议进行转换,但数据是相同的尝试使用如下查询:
SELECT location_id, sum(case when HOUR(time) = 0 then 1 else 0) as '0',
sum(case when HOUR(time) = 1 then 1 else 0) as '1',-- till 23
FROM table
GROUP BY location_id
尝试使用如下查询:
SELECT location_id, sum(case when HOUR(time) = 0 then 1 else 0) as '0',
sum(case when HOUR(time) = 1 then 1 else 0) as '1',-- till 23
FROM table
GROUP BY location_id