MySQL query question
Moderator: General Moderators
-
matthiasone
- Forum Contributor
- Posts: 117
- Joined: Mon Jul 22, 2002 12:14 pm
- Location: Texas, USA
- Contact:
MySQL query question
How would you write a query to find a record whose date field is closest to the current date?

- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Code: Select all
SELECT *, ABS(UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP( your_date_field )) `diff` FROM table ORDER BY `diff` LIMIT 1