开发者

Working with Dates in SQL

I have an events table and need to pull the 4 closest dates to tod开发者_如何学Pythonay's date and they can be in the past, present or future.

What would the SQL (using MySQL) be for this if it is possible?

Thanks

Brett


I don't know which DB you are using, but this works with mysql:

select *
from event
order by abs(datediff(event_date, now()))
limit 4


Try using the TIMEDIFF function like this:

select * 
from events
order by abs(timediff(now(), yourdatecolumn))
limit 4;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜