I have an art gallery page featuring a panel that shows up to 5 exhibitions ‘At this gallery’. Sometimes there could be many on, it should show the current or upcoming first, then resort to past exhibitions.
Is it possible to do in one query?
The following will give me those closest in terms of date difference but I can end up getting past events at the top of the list (because if it’s a day past, it’s obviously less than the upcoming event in 5 days)
SELECT DATEDIFF(date_start, NOW()) as date_diff, exhibitions.*
FROM events
WHERE event.gallery_id = XX
ORDER BY date_diff asc
LIMIT 5
The date_diff is returning negative values for past events. This is one fix: