我有一个名为startTime
的字段,其类型为DATETIME
。我每小时都有一些数据(每小时1行)。我想获取特定小时的最后7天(例如:01:00:00
)。所以基本上,查询应该只返回7行。
我怎样才能在MySQL中做到这一点?
select traffic from incomingTraffic WHERE ...
示例数据:
+---------------------+---------------------+-----------+----------+
| startTime | endTime | component | traffic |
+---------------------+---------------------+-----------+----------+
| 2015-05-01 00:00:00 | 2015-05-01 01:00:00 | rest | 29090345 |
| 2015-05-01 01:00:00 | 2015-05-01 02:00:00 | rest | 32224087 |
| 2015-05-01 02:00:00 | 2015-05-01 03:00:00 | rest | 35165799 |
| 2015-05-01 03:00:00 | 2015-05-01 04:00:00 | rest | 36903464 |
| 2015-05-01 04:00:00 | 2015-05-01 05:00:00 | rest | 40394130 |
| 2015-05-01 05:00:00 | 2015-05-01 06:00:00 | rest | 44874862 |
| 2015-05-01 06:00:00 | 2015-05-01 07:00:00 | rest | 49988600 |
| 2015-05-01 07:00:00 | 2015-05-01 08:00:00 | rest | 52240544 |
| 2015-05-01 08:00:00 | 2015-05-01 09:00:00 | rest | 54517705 |
| 2015-05-01 09:00:00 | 2015-05-01 10:00:00 | rest | 55277967 |
| 2015-05-01 10:00:00 | 2015-05-01 11:00:00 | rest | 55285309 |
| 2015-05-01 11:00:00 | 2015-05-01 12:00:00 | rest | 55572614 |
| 2015-05-01 12:00:00 | 2015-05-01 13:00:00 | rest | 54507826 |
| 2015-05-01 13:00:00 | 2015-05-01 14:00:00 | rest | 52430349 |
| 2015-05-01 14:00:00 | 2015-05-01 15:00:00 | rest | 47084995 |
| 2015-05-01 15:00:00 | 2015-05-01 16:00:00 | rest | 41671868 |
| 2015-05-01 16:00:00 | 2015-05-01 17:00:00 | rest | 37492018 |
| 2015-05-01 17:00:00 | 2015-05-01 18:00:00 | rest | 34391610 |
| 2015-05-01 18:00:00 | 2015-05-01 19:00:00 | rest | 33814871 |
| 2015-05-01 19:00:00 | 2015-05-01 20:00:00 | rest | 32466192 |
| 2015-05-01 20:00:00 | 2015-05-01 21:00:00 | rest | 28703534 |
| 2015-05-01 21:00:00 | 2015-05-01 22:00:00 | rest | 25564237 |
| 2015-05-01 22:00:00 | 2015-05-01 23:00:00 | rest | 23209539 |
| 2015-05-01 23:00:00 | 2015-05-02 00:00:00 | rest | 24853381 |
| 2015-05-02 00:00:00 | 2015-05-02 01:00:00 | rest | 24090 |
| 2015-05-02 00:00:00 | 2015-05-02 01:00:00 | rest | 26848485 |
| 2015-05-02 01:00:00 | 2015-05-02 02:00:00 | rest | 28507177 |
| 2015-05-02 02:00:00 | 2015-05-02 03:00:00 | rest | 29970856 |
| 2015-05-02 03:00:00 | 2015-05-02 04:00:00 | rest | 30607213 |
| 2015-05-02 04:00:00 | 2015-05-02 05:00:00 | rest | 34005454 |
| 2015-05-02 05:00:00 | 2015-05-02 06:00:00 | rest | 38241748 |
| 2015-05-02 06:00:00 | 2015-05-02 07:00:00 | rest | 42154837 |
| 2015-05-02 07:00:00 | 2015-05-02 08:00:00 | rest | 44181502 |
| 2015-05-02 08:00:00 | 2015-05-02 09:00:00 | rest | 45787319 |
| 2015-05-02 09:00:00 | 2015-05-02 10:00:00 | rest | 47283634 |
| 2015-05-02 10:00:00 | 2015-05-02 11:00:00 | rest | 47328065 |
| 2015-05-02 11:00:00 | 2015-05-02 12:00:00 | rest | 47370131 |
| 2015-05-02 12:00:00 | 2015-05-02 13:00:00 | rest | 46298305 |
| 2015-05-02 13:00:00 | 2015-05-02 14:00:00 | rest | 43894081 |
| 2015-05-02 14:00:00 | 2015-05-02 15:00:00 | rest | 40307217 |
| 2015-05-02 15:00:00 | 2015-05-02 16:00:00 | rest | 35481808 |
| 2015-05-02 16:00:00 | 2015-05-02 17:00:00 | rest | 32316694 |
| 2015-05-02 17:00:00 | 2015-05-02 18:00:00 | rest | 30426755 |
| 2015-05-02 18:00:00 | 2015-05-02 19:00:00 | rest | 29809260 |
| 2015-05-02 19:00:00 | 2015-05-02 20:00:00 | rest | 28466543 |
| 2015-05-02 20:00:00 | 2015-05-02 21:00:00 | rest | 25392149 |
| 2015-05-02 21:00:00 | 2015-05-02 22:00:00 | rest | 21182582 |
| 2015-05-02 22:00:00 | 2015-05-02 23:00:00 | rest | 20954845 |
答案 0 :(得分:2)
这个问题比它看起来更棘手,因为它取决于数据的更新方式以及OP在过去7天内的真实含义"。
如果您想要相对于当前日期的最后7天,请使用:
WHERE hour(startTime) = 7 AND
startTime >= date_sub(CURDATE(), interval 7 day)
现在,这可能会让您获得7天或8天的时间,具体取决于数据的更新方式和当前时间。您可以添加ORDER BY startTime DESC LIMIT 7
以获得7天。
如果您想在表格中显示最近7天,请尝试以下方法:
SELECT traffic
FROM incomingTraffic
WHERE hour(startTime) = 7
ORDER BY startTime DESC
LIMIT 7;
这将获得最近7天的数据。但是,如果您有一个大表,这可能是低效的。在WHERE
上使用startTime
子句可以提高查询效率。
答案 1 :(得分:0)
除了数据应该小于7天的情况外,添加条件hour()=1
SELECT traffic FROM incomingTraffic
WHERE hour(startTime)=1 AND startTime>ADDDATE(NOW(), -7);
抱歉,花了一些时间收集其他位并让它们正确; - )
答案 2 :(得分:0)
您可以使用DATE_SUB
operation获得7天,并将小时与HOUR()
相匹配,从而为您提供日期的小时部分。
SELECT startTime, traffic FROM incomingTraffic
WHERE
startTime > DATE_SUB('2015-05-01', INTERVAL 7 DAY)
AND HOUR(startTime) = 1;
请注意,我没有NOW()
,因为您的示例数据已经过去了。另请注意,您应首先检查日期范围,然后执行小时比较,因为这样会更便宜,因为mysql需要进行转换以检查小时。