Created
December 13, 2011 22:24
-
-
Save samflores/1474193 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT e.id, ( | |
SELECT CASE periodicity | |
WHEN 'daily' THEN DATEDIFF('2011-01-08', s.starts_at) <= s.occurrences -1 | |
WHEN 'weekly' THEN DAYOFWEEK('2011-01-08') = DAYOFWEEK(s.starts_at) AND '2011-01-08' < ADDDATE(s.starts_at, INTERVAL s.occurrences -1 WEEK) | |
WHEN 'bi-weekly' THEN DATEDIFF('2011-01-08', s.starts_at) MOD 14 = 0 AND '2011-01-08' < ADDDATE(s.starts_at, INTERVAL s.occurrences -1 WEEK) | |
WHEN 'monthly' THEN DAY('2011-01-08') = DAY(s.starts_at) AND '2011-01-08' < ADDDATE(s.starts_at, INTERVAL s.occurrences -1 MONTH) | |
WHEN 'yearly' THEN DAY('2011-01-08') = DAY(s.starts_at) AND MONTH('2011-01-08') = MONTH(s.starts_at) AND '2011-01-08' < ADDDATE(s.starts_at, INTERVAL s.occurrences - 1 YEAR) | |
END) AS matches | |
FROM events AS e | |
INNER JOIN schedules AS s ON (s.event_id = e.id) | |
HAVING matches = 1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment