我正在尝试从数据库中计算出每小时的点击次数。数据基本上存储如下:
Table Name: Visitors
============================
VisitorIP TIMESTAMP
----------------------------
15.215.65.65 123456789
我想显示每小时的总点击次数(在过去的6小时内),包括没有点击的小时数。 输出示例:
// Assuming now : 21:00
21:00 - 0 hits
20:00 - 1 hits
19:00 - 4 hits
18:00 - 0 hits
17:00 - 2 hits
16:00 - 3 hits
我很想将数据作为数组获取,请注意存储的日期是UNIX时间戳格式。并且可能有几个小时没有任何命中!
由于
答案 0 :(得分:4)
SELECT MONTH(timestamp),DAY(timestamp), HOUR(timestamp),count(*)
from visitors
group by MONTH(timestamp), DAY(timestamp), HOUR(timestamp)
在php中,您需要考虑数据中的任何漏洞。
答案 1 :(得分:3)
您可以使用简单的关键字BETWEEN。您可以描述日期时间介于您要选择的值之间(范围3600秒)。
SELECT * FROM VISITORS WHERE TIMESTAMP BETWEEN UNIX_TIMESTAMP()-3600*1 AND UNIX_TIMESTAMP()-3600*0;
SELECT * FROM VISITORS WHERE TIMESTAMP BETWEEN UNIX_TIMESTAMP()-3600*2 AND UNIX_TIMESTAMP()-3600*1;
SELECT * FROM VISITORS WHERE TIMESTAMP BETWEEN UNIX_TIMESTAMP()-3600*3 AND UNIX_TIMESTAMP()-3600*2;
SELECT * FROM VISITORS WHERE TIMESTAMP BETWEEN UNIX_TIMESTAMP()-3600*4 AND UNIX_TIMESTAMP()-3600*3;
SELECT * FROM VISITORS WHERE TIMESTAMP BETWEEN UNIX_TIMESTAMP()-3600*5 AND UNIX_TIMESTAMP()-3600*4;
SELECT * FROM VISITORS WHERE TIMESTAMP BETWEEN UNIX_TIMESTAMP()-3600*6 AND UNIX_TIMESTAMP()-3600*5;
答案 2 :(得分:0)
刚刚简化了以上答案,
for($i=1;$i<=6;$i++){
$s -= $i;
$query[$i] = "SELECT * FROM VISITORS WHERE TIMESTAMP BETWEEN UNIX_TIMESTAMP()-3600*$i AND UNIX_TIMESTAMP()-3600*$s";
}
$i = 1;
foreach($query as $value){
$result[$i] = mysql_fetch_row($query[$i]);
$i++;
}
只是我的简单想法。随意使用。