从MySQL表获取最近的日期 [英] Get closest date from MySQL table

查看:97
本文介绍了从MySQL表获取最近的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从MySQL表中获得与当前日期最接近的日期。

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

这是我的表:

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

所以如果查询今天运行,它将返回 1 | 2012-10-29 |测试

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

任何帮助都非常感激。谢谢

Any help is much appreciated. Thanks

推荐答案

SELECT 
  * 
FROM 
  your_table 
ORDER BY 
  ABS(DATEDIFF(NOW(), `date`))
LIMIT 1

这篇关于从MySQL表获取最近的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆