I’m building a web application for location-based check ins, sort of like a local 4square, but based on RFID tags.
Anyway, each check-in is stored in a MySQL table with a userID and the time of the check-in as a DATETIME column.
Now I’d like to show which users have the closest check-in times between different stations.
Explanation: Let’s say user A checked in at 21:43:12 and then again at 21:43:19. He moved between stations in 7 seconds.
There are thousands of check-ins in the database, how do I write SQL to select the users with the two closest check-in times?
Really fast solution would introduce some precalculations. Like storing the difference between current and previous checkins.
In this case you would select what you need in fast manner (as long as you cover that column by index).
Not using precalculation in this case would cause terrible queries that would operate over cartesian-like productions.