Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inexact searching for nearest time value

I'm looking for a programmatically and syntactically efficient way to go about searching for a value based on a Timestamp. Essentially I want to find the closest timestamp match...

Say for example in a MySQL table I have:

ID    TIME    Blob
1    4:03:10    abc
2    4:04:30    def
3    4:04:45    ghi

And I want to query this table based on the time 4:04:40. I would want to return record ID #3... If I searched for 4:04:35 I would want to return ID #2... How do I go about implementing this? I have many millions of rows in this table and I was thinking something like levenshtein dist will be too slow..?

Cheers!

like image 631
NightWolf Avatar asked Jan 19 '23 04:01

NightWolf


1 Answers

How about:

SELECT id, MIN(ABS(time(time) - time("4:04:35"))) FROM table
like image 95
MRAB Avatar answered Jan 29 '23 07:01

MRAB