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
  • 158,678
  • 38
  • 247
  • 274
CharliePrynn
  • 3,034
  • 5
  • 40
  • 68

3 Answers3

15
SELECT 
  * 
FROM 
  your_table 
ORDER BY 
  ABS(DATEDIFF(NOW(), `date`))
LIMIT 1
xdazz
  • 158,678
  • 38
  • 247
  • 274
0
select top 1 date from table
where date > now()
order by date desc
0xCAFEBABE
  • 5,576
  • 5
  • 34
  • 59
0
SELECT * FROM `your_table` WHERE ABS(DATEDIFF(`date`, NOW()));

Returns:

'1', '2012-10-29 00:00:00', 'test'
LeonardChallis
  • 7,759
  • 6
  • 45
  • 76