开发者

Working with Dates in SQL

开发者 https://www.devze.com 2023-04-04 12:24 出处:网络
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.

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

精彩评论

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