Fox Fox - 2 months ago 7
MySQL Question

Get records which have time filed in next 5 min

I have to get all the records that have the time filed in next 5 minutes. I have written query -- but it does not get any records even if present.

SELECT *
FROM `table`
WHERE time <= UTC_TIME() + INTERVAL 5
MINUTE AND time >= UTC_TIME()


The time is in the form of "23:22:00". In this example I need to get all the records that have time less than now(23:22:00) and (23:27:00). What am I missing?

Thanks

Answer

Using only time you can have the following

select * from `table` where timediff (utc_time, time) < '00:05:00';

Without a date you will have 23:55:00 to 23:59:59 overflow problems. I'd consider using a datetime or unixtime instead if you have any control over the database.