Brett
Brett

Reputation: 887

Working with Dates in SQL

I have an events table and need to pull the 4 closest dates to today'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

Upvotes: 3

Views: 115

Answers (2)

Derek
Derek

Reputation: 23318

Try using the TIMEDIFF function like this:

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

Upvotes: 0

Bohemian
Bohemian

Reputation: 425378

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

Upvotes: 5

Related Questions