Skip to content

Instantly share code, notes, and snippets.

@si
Created January 17, 2013 12:17
Show Gist options
  • Save si/4555566 to your computer and use it in GitHub Desktop.
Save si/4555566 to your computer and use it in GitHub Desktop.
Return next five birthdays from user's date of birth
SELECT first_name, last_name, dob, DATEDIFF(next_birthday, NOW()) AS distance
FROM (
SELECT *, ADDDATE(birthday, INTERVAL birthday < DATE(NOW()) YEAR) AS next_birthday
FROM (
SELECT *, ADDDATE(dob, INTERVAL YEAR(NOW()) - YEAR(dob) YEAR) AS birthday
FROM users
WHERE active = 1 AND dob IS NOT NULL
) AS T1
) AS T2
ORDER BY distance ASC
LIMIT 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment