我遇到了每隔一小时获取记录数的问题。
我的表结构是
CREATE TABLE `statistics` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`uuid` VARCHAR(100) NULL DEFAULT NULL,
`type_id` VARCHAR(50) NULL DEFAULT NULL,
`in_time` TIMESTAMP NULL DEFAULT NULL,
`out_time` TIMESTAMP NULL DEFAULT NULL,
`error_id` INT(10) UNSIGNED ZEROFILL NULL DEFAULT NULL,
`status` TINYINT(4) UNSIGNED NULL DEFAULT NULL,
`last_updated` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
)
ENGINE=MyISAM
AUTO_INCREMENT=1594;
我尝试了这样的查询:
SELECT FLOOR( TIME_TO_SEC( TIMEDIFF( NOW(), last_updated)) / 3600 ) HOURS,COUNT(*),error_id FROM statistics WHERE last_updated > DATE_SUB(NOW(), INTERVAL 24 HOUR) GROUP BY HOUR(last_updated) and error_id != 0 ORDER BY last_updated DESC;
输出是这样的
+-------+----------+
| HOURS | COUNT(*) |
+-------+----------+
| 1 | 205 |
| 2 | 70 |
| 3 | 14 |
| 4 | 10 |
| 5 | 16 |
| 7 | 29 |
| 8 | 412 |
| 9 | 65 |
| 10 | 79 |
| 11 | 149 |
| 12 | 317 |
| 13 | 306 |
| 14 | 356 |
| 15 | 597 |
| 16 | 433 |
| 17 | 186 |
| 18 | 527 |
| 19 | 349 |
| 20 | 397 |
| 21 | 480 |
| 22 | 460 |
| 23 | 526 |
| 23 | 411 |
+-------+----------+
但这是不正确的。因为相同的小时数(23)是不同的计数。提前致谢。