Get closest date from MySQL table

I need to get the closest date to the 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 request was launched today, it would return 1 | 2012-10-29 | test

Any help is greatly appreciated. Thanks

+3
source share
3 answers
SELECT 
  * 
FROM 
  your_table 
ORDER BY 
  ABS(DATEDIFF(NOW(), `date`))
LIMIT 1
+14
source
select top 1 date from table
where date > now()
order by date desc
0
source
SELECT * FROM `your_table` WHERE ABS(DATEDIFF(`date`, NOW()));

Return:

'1', '2012-10-29 00:00:00', 'test'
0
source

Source: https://habr.com/ru/post/1606685/


All Articles