MySQL query question

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
matthiasone
Forum Contributor
Posts: 117
Joined: Mon Jul 22, 2002 12:14 pm
Location: Texas, USA
Contact:

MySQL query question

Post by matthiasone »

How would you write a query to find a record whose date field is closest to the current date?

8O
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

SELECT *, ABS(UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP( your_date_field )) `diff` FROM table ORDER BY `diff` LIMIT 1
I think
Post Reply