SQL Query to show nearest date?

后端 未结 5 764
予麋鹿
予麋鹿 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:30

    SELECT event_id 
    FROM Table 
    ORDER BY ABS( DATEDIFF( EVENT_START_DATE, NOW() ) ) 
    LIMIT 3
    

    The ABS() means that an event 1 day ago is just as close as an event 1 day in the future. If you only want events that haven't happened yet, do

    SELECT event_id 
    FROM Table 
    WHERE EVENT_START_DATE > NOW() 
    ORDER BY EVENT_START_DATE 
    LIMIT 3 
    

提交回复
热议问题