I researched this a lot but I couldn't find anything that comes close to my issue so I am posting scenario for assistance and guidance on find a method - just point me in right direction if possible. Unlike some comments below, I don't quite know how to provide a minimum reproducible query especially since I don't know if its even possible.
I have a mysql table that holds Event records. Each record defines when an Event is to start (startDate
) - example: Sept 4, 2019 (Wednesday). The Event is supposed to repeat every Wednesday after that - but there is only the ONE master Event record. eventFreq
designates how often the Event is supposed to occur.
eventFreq = 1 ; // every x day of week (every Wednesday)
eventFreq = 2 ; // every other x day of week (every other Monday)
eventFreq = 3 ; // every 3rd X day of month (every 3rd Tuesday)
Is it possible to write a mysql query that would, based on the master record startDate
and the eventFreq
, give me all instances of Wednesdays in a future month (IE: All Wednesdays in March 2020)? Or a query that would give me the NEXT Wednesday based on todays current date. These queries would need to figure out that startDate
is a Wednesday to begin with in order to determine future ones.
If it determined a future Wednesday in the month of March 2020, it would return the master Event record plus an extra field for the specific future Wednesday.
Master Event Record: ID,eventName,eventDescription,startDate,eventFreq,blah1,blah2,blah3
If a Wednesday in March was found, it would return the entire record with an added column nextDate
as one of the Wednesdays in March. thus it would return me the same four records with an appended nextDate
column.
Crudely:
IF DATE == Wednesday, then Date as nextDate
OR IF Wednesday BETWEEN FirstDayOfMonth AND LastDayOfMonth
OR IF Wednesday, then Next Wednesday after today
Would return the same Master record X times with an appended column nextDate
:
ID,eventName,eventDescription,startDate,eventFreq,blah1,blah2,blah3,nextDate // March 4 2020
ID,eventName,eventDescription,startDate,eventFreq,blah1,blah2,blah3,nextDate // March 11 2020
ID,eventName,eventDescription,startDate,eventFreq,blah1,blah2,blah3,nextDate // March 18 2020
ID,eventName,eventDescription,startDate,eventFreq,blah1,blah2,blah3,nextDate // March 25 2020
Currently I am extracting all Events and using PHP to process all of the Events startDate
and eventFreq
(every Wednesday) to determine future Wednesdays. The code is extensive and complicated for all the various scenarios - so I was hoping a good mysql query would just return the proper future dates and then all I have to do is process the results.
Can this be achieved?