Created
November 22, 2019 05:13
-
-
Save Adizbek/feb55f22c478a42cbb8cde0ab1f7b26f to your computer and use it in GitHub Desktop.
Get nearest birthdays
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 * FROM (SELECT * FROM ( | |
SELECT *, 1 as o FROM schoolid.pupils p | |
WHERE month(now()) < month(p.birth_date) OR (month(now()) = month(p.birth_date) AND dayofmonth(now()) <= dayofmonth(p.birth_date)) | |
ORDER BY | |
(month(p.birth_date)) asc, | |
(dayofmonth(p.birth_date)) asc) cx | |
UNION | |
SELECT * FROM ( | |
SELECT *, 2 as o FROM schoolid.pupils p | |
WHERE month(now()) > month(p.birth_date) OR (month(now()) = month(p.birth_date) AND dayofmonth(now()) > dayofmonth(p.birth_date)) | |
ORDER BY | |
(month(p.birth_date)) asc, | |
(dayofmonth(p.birth_date)) asc) dx) f | |
ORDER BY f.o ASC, month(f.birth_date) asc, dayofmonth(f.birth_date) asc | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment