我想获取一组时间戳(来自mySQL DATE_TIME)并返回最后说24小时内每小时的出现次数。基本上我正在尝试制作一个显示最近数据库活动量的实时图表。
示例结果可能是..
//4 occurrences between now and one hour ago
$hour1=4;
//2 occurrences between 2 and 3 hours ago
$hour2=2;
//7 occurrences between 3 and 4 hours ago
$hour3=7;
有什么想法吗?
答案 0 :(得分:3)
这样的东西?
SELECT ..., count(*) AS occurences, hour(timestampfield) AS hour
FROM ...
WHERE timestampfield >= (now() - INTERVAL 24 HOUR)
GROUP BY hour(timestampfield)
获取过去24小时内所有事件的计数,按其出现的小时分组。
答案 1 :(得分:1)
为什么不在MySQL表中添加一个字段来存储DATETIME小时值和count()次出现的分组?