SQL Query to show nearest date?

后端 未结 5 751
予麋鹿
予麋鹿 2020-11-27 15:46

I\'m trying to figure out how to write a MySQL query that will return the closest 3 events in terms of date.

This is my table:

EVENT_ID    EVENT_NAME         


        
5条回答
  •  失恋的感觉
    2020-11-27 16:26

    The query from accepted answer actually just sort previously selected values, not filter them before select. But this query works for me:

    SELECT event_id, event_date
    FROM events 
    WHERE ABS(TIMESTAMPDIFF(DAY, event_date, $some_date)) < 10
    ORDER BY event_date
    

    Explanation: number 10 is a day range (both after and before). Without ABS() you can select only previous or future events, but I needed the closest.

提交回复
热议问题