You can use TIMESTAMPDIFF to group by time intervals:
For a given interval of hours, you can use:
SELECT '2012-08-03 00:00:00' + INTERVAL FLOOR(TIMESTAMPDIFF(HOUR, '2012-08-03 00:00:00', timestamp) / <n>) * <n> HOUR AS start_time, COUNT(*) AS total FROM event WHERE timestamp >= '2012-08-03 00:00:00' GROUP BY start_time
Replace events 2012-08-03 00:00:00 with the minimum entry date.
<n> is your specified interval in hours (every 2 hours, 3 hours, etc.), and you can do the same in minutes:
SELECT '2012-08-03 00:00:00' + INTERVAL FLOOR(TIMESTAMPDIFF(MINUTE, '2012-08-03 00:00:00', timestamp) / <n>) * <n> MINUTE AS start_time, COUNT(*) AS total FROM event WHERE timestamp >= '2012-08-03 00:00:00' GROUP BY start_time
Where <n> is your specified interval in minutes (every 45 minutes, 90 minutes, etc.).
Make sure that you pass the minimum input date (in this example 2012-08-03 00:00:00 ) as the second parameter TIMESTAMPDIFF .
EDIT: If you do not want to worry about which intermediate block to select in the TIMESTAMPDIFF function, then of course just do the interval in seconds (300 = 5 minutes, 3600 = 1 hour, 7200 = 2 hours, etc.)
SELECT '2012-08-03 00:00:00' + INTERVAL FLOOR(TIMESTAMPDIFF(SECOND, '2012-08-03 00:00:00', timestamp) / <n>) * <n> SECOND AS start_time, COUNT(*) AS total FROM event WHERE timestamp >= '2012-08-03 00:00:00' GROUP BY start_time
EDIT2: To respond to your comment regarding reducing the number of areas in an instruction in which you must pass on your minimum parameter date, you can use:
SELECT b.mindate + INTERVAL FLOOR(TIMESTAMPDIFF(SECOND, b.mindate, timestamp) / <n>) * <n> SECOND AS start_time, COUNT(*) AS total FROM event JOIN (SELECT '2012-08-03 00:00:00' AS mindate) b ON timestamp >= b.mindate GROUP BY start_time
And just pass your minimum datetime parameter once per subsect. connections.
You can even make the second column in the join subselect for the interval of seconds (e.g. 3600 ) and name the column something like secinterval ... then change <n> to b.secinterval so that you only have to go through your minimum date parameter and interval one every time.
SQLFiddle Demo