skip to Main Content

My table has events with start and end dates. I need to select only the events for the coming weekend.

For events of the upcoming week I can just use as it’s just the same as the selection for today.

SELECT * FROM table WHERE startdate <= CURDATE() AND enddate >= CURDATE()

How can I select only the events for the upcoming weekend?

2

Answers


  1. You can calculate next Saturday and Sunday using weekday function and some math:

    Mon (weekday = 0) => add 5 days to get next Sat
    Tue (weekday = 1) => add 4 days to get next Sat
    .
    .
    .
    Sun (weekday = 6) => add -1 days to get previous Sat
    

    And where clause will look like (ref):

    WHERE current_date + interval (5 - weekday(current_date)) day <= enddate
    and   current_date + interval (6 - weekday(current_date)) day >= startdate
    
    Login or Signup to reply.
  2. ...
    WHERE CURRENT_DATE + INTERVAL (5 - WEEKDAY(CURRENT_DATE)) DAY <= enddate
      AND CURRENT_DATE + INTERVAL (6 - WEEKDAY(CURRENT_DATE)) DAY >= startdate
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search