Skip to content

Instantly share code, notes, and snippets.

@rrreeeyyy
Created June 26, 2014 15:31
Show Gist options
  • Save rrreeeyyy/39b5cd0a625620f3c032 to your computer and use it in GitHub Desktop.
Save rrreeeyyy/39b5cd0a625620f3c032 to your computer and use it in GitHub Desktop.
SELECT
e.id AS event_id,
e.name,
s1.id AS schedule_id,
date_format(s1.due_date, '%Y/%m/%d') AS due_date
FROM events e
LEFT JOIN schedules s1
ON e.id = s1.event_id
WHERE s1.id IS NULL
OR s1.id = (
SELECT s2.id
FROM schedules s2
WHERE s2.event_id = e.id
AND s2.due_date >= (SELECT max(sysdate) FROM sysdate_dummy)
ORDER BY
s2.due_date
LIMIT 1
)
ORDER BY
s1.id IS NULL,
s1.due_date,
e.id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment