Skip to content

Instantly share code, notes, and snippets.

@marcosgz
Created May 11, 2012 21:10
Show Gist options
  • Save marcosgz/2662433 to your computer and use it in GitHub Desktop.
Save marcosgz/2662433 to your computer and use it in GitHub Desktop.
SQL => /browse/concert/dates:this-weekend
Event Load (21.7ms)
SELECT `events`.* FROM `events`
INNER JOIN `schedules` ON `schedules`.`event_id` = `events`.`id`
WHERE `events`.`deleted` = 0 AND ((ends_at >= '2012-05-13 23:59:59' OR ends_at IS NULL) AND ((frequency IS NULL AND DATE(starts_at) IN ('2012-05-11','2012-05-12','2012-05-13')) OR (starts_at <= '2012-05-11 00:00:00'
AND (
frequency = 'daily'
OR (
frequency = 'weekly'
AND dayname = DATE_FORMAT('2012-05-11', '%W')
)
OR (
frequency = 'bi-weekly'
AND DATEDIFF('2012-05-11', starts_at) MOD 14 = 0
)
OR (
frequency = 'monthly'
AND dayname = DATE_FORMAT('2012-05-11', '%W')
AND (
LEFT(occurrence, 1) = (WEEK('2012-05-11', 5) - WEEK(DATE_SUB('2012-05-11', INTERVAL DAYOFMONTH('2012-05-11')-1 DAY), 5) + 1)
OR (
occurrence = 'last'
AND MONTH('2012-05-11') < MONTH(DATE_ADD('2012-05-11', INTERVAL 7 DAY))
)
)
)
) OR starts_at <= '2012-05-11 00:00:00'
AND (
frequency = 'daily'
OR (
frequency = 'weekly'
AND dayname = DATE_FORMAT('2012-05-12', '%W')
)
OR (
frequency = 'bi-weekly'
AND DATEDIFF('2012-05-12', starts_at) MOD 14 = 0
)
OR (
frequency = 'monthly'
AND dayname = DATE_FORMAT('2012-05-12', '%W')
AND (
LEFT(occurrence, 1) = (WEEK('2012-05-12', 5) - WEEK(DATE_SUB('2012-05-12', INTERVAL DAYOFMONTH('2012-05-12')-1 DAY), 5) + 1)
OR (
occurrence = 'last'
AND MONTH('2012-05-12') < MONTH(DATE_ADD('2012-05-12', INTERVAL 7 DAY))
)
)
)
) OR starts_at <= '2012-05-11 00:00:00'
AND (
frequency = 'daily'
OR (
frequency = 'weekly'
AND dayname = DATE_FORMAT('2012-05-13', '%W')
)
OR (
frequency = 'bi-weekly'
AND DATEDIFF('2012-05-13', starts_at) MOD 14 = 0
)
OR (
frequency = 'monthly'
AND dayname = DATE_FORMAT('2012-05-13', '%W')
AND (
LEFT(occurrence, 1) = (WEEK('2012-05-13', 5) - WEEK(DATE_SUB('2012-05-13', INTERVAL DAYOFMONTH('2012-05-13')-1 DAY), 5) + 1)
OR (
occurrence = 'last'
AND MONTH('2012-05-13') < MONTH(DATE_ADD('2012-05-13', INTERVAL 7 DAY))
)
)
)
)))) GROUP BY events.id
@marcosgz
Copy link
Author

mysql> select * from schedules where event_id = 1000002077;
+--------+---------------------+------------+----------+---------+-----------+---------+---------------------+------------+
| id | starts_at | event_id | duration | comment | frequency | dayname | ends_at | occurrence |
+--------+---------------------+------------+----------+---------+-----------+---------+---------------------+------------+
| 188821 | 2012-05-12 22:00:00 | 1000002077 | | NULL | NULL | NULL | 2012-05-12 22:00:00 | NULL |
+--------+---------------------+------------+----------+---------+-----------+---------+---------------------+------------+
1 row in set (0.00 sec)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment