SQL-查找最接近当前日期的日期 [英] SQL - Find date closest to current date

查看:1036
本文介绍了SQL-查找最接近当前日期的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的SQL查询遇到一些麻烦. 我得到了这张桌子:

I'm having some trouble with my SQL query. I got this table:

insert into Table1 (date, personssn)
insert ('2012-01-21 12:01:33', '123456789');
insert into Table1 (date, personssn)
insert ('2012-02-22 12:01:33', '123456789');

问题是我想选择日期离当前日期最近的人.我一直在使用"CURDATE()",但似乎无法使其正常工作.有人可以在正确的方向帮助我吗?

The problem is that I want to select the personssn who have a date CLOSEST to the current date. I've been working with "CURDATE()" but can't seem to get it to work. Anyone that can help me in the right direction?

谢谢.

推荐答案

select  *
from    Table1
order by
        abs(now() - date) desc
limit   1

这篇关于SQL-查找最接近当前日期的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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