4

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

xdazz
  • 154,648
  • 35
  • 237
  • 264
CharliePrynn
  • 2,924
  • 5
  • 37
  • 66

3 Answers3

15
SELECT 
  * 
FROM 
  your_table 
ORDER BY 
  ABS(DATEDIFF(NOW(), `date`))
LIMIT 1
xdazz
  • 154,648
  • 35
  • 237
  • 264
0
select top 1 date from table
where date > now()
order by date desc
0xCAFEBABE
  • 5,418
  • 5
  • 33
  • 56
0
SELECT * FROM `your_table` WHERE ABS(DATEDIFF(`date`, NOW()));

Returns:

'1', '2012-10-29 00:00:00', 'test'
LeonardChallis
  • 7,550
  • 6
  • 44
  • 76