MySQL - 从timestamp列选择每2小时的间隔

时间:2015-12-14 15:37:01

标签: mysql sql

如何在时间戳列中选择每n小时?我可以通过分组1小时来完成:

SELECT log_timestamp
FROM log
GROUP BY 
    DATE( log_timestamp ), 
    HOUR( log_timestamp )

返回:

log_timestamp
2015-11-08 00:00:01
2015-11-08 01:00:01
2015-11-08 02:00:01
2015-11-08 03:00:01
...

关于如何在2小时或更长时间内完成此操作的任何想法?

log_timestamp
2015-11-08 00:00:01
2015-11-08 02:00:01
2015-11-08 04:00:01
2015-11-08 06:00:01
...

1 个答案:

答案 0 :(得分:6)

您可以使用INTEGER DIVISION

SELECT L.log_timestamp 
FROM log L 
GROUP BY DATE( log_timestamp ), hour( log_timestamp ) DIV 2

SqlFiddleDemo