Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get closest date from MySQL table

Tags:

date

sql

php

mysql

I need to get the closest date to current date from a MySQL table.

This is my table:

id        | date          | name
1         | 2012-10-29    | test
2         | 2009-11-31    | test

So if the query was run today, it would return 1 | 2012-10-29 | test

Any help is much appreciated. Thanks

like image 217
CharliePrynn Avatar asked Dec 06 '22 11:12

CharliePrynn


1 Answers

SELECT 
  * 
FROM 
  your_table 
ORDER BY 
  ABS(DATEDIFF(NOW(), `date`))
LIMIT 1
like image 86
xdazz Avatar answered Dec 10 '22 12:12

xdazz