我有一个名为uploads(id,file,date)
的表,其中date
是DATETIME
,我想在我的网站上显示最近6小时上传的统计信息板。
类似的东西:
13:00 : 12,200 Uploads
14:00 : 30,455 Uploads
15:00 : 15,202 Uploads
16:00 : 61,014 Uploads
17:00 : 55,104 Uploads
18:00 : 43,019 Uploads
由于
答案 0 :(得分:5)
使用:
SELECT DATE_FORMAT(u.date, '%H:00') AS hr,
COUNT(*) AS numUploads
FROM UPLOADS u
WHERE u.date >= DATE_SUB(NOW(), INTERVAL 6 HOUR)
GROUP BY DATE_FORMAT(u.date, '%H:00')
ORDER BY hr
...将返回:
hr numUploads
------------------
13:00 12200
14:00 30455
15:00 15202
16:00 61014
17:00 55104
18:00 43019
NOW()
包括运行查询时的时间部分。意思是,它可能是一小时后的31分钟,因此值可能会在显示的最后一小时内发生变化。$query = "SELECT DATE_FORMAT(u.date, '%H:00') AS hr,
COUNT(*) AS numUploads
FROM UPLOADS u
WHERE u.date >= DATE_SUB(NOW(), INTERVAL 6 HOUR)
GROUP BY DATE_FORMAT(u.date, '%H:00')"
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)) {
echo $row['hr'] .' : '. NUMBER_FORMAT($row['numUploads']) .' Uploads'
}