Created
January 17, 2013 12:17
-
-
Save si/4555566 to your computer and use it in GitHub Desktop.
Return next five birthdays from user's date of birth
This file contains 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 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