Skip to content

Instantly share code, notes, and snippets.

@Adizbek
Created November 22, 2019 05:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Adizbek/feb55f22c478a42cbb8cde0ab1f7b26f to your computer and use it in GitHub Desktop.
Save Adizbek/feb55f22c478a42cbb8cde0ab1f7b26f to your computer and use it in GitHub Desktop.
Get nearest birthdays
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