MySQL Upcoming Birthdays Query

When I try to write a query for upcoming birthdays using MYSql, I get the below query to fetch the upcoming birthdays from the employee list. It may be useful to you.

select * from (
	select id,  datediff(DATE_FORMAT(date_of_birth,concat('%',YEAR(CURDATE()),'-%m-%d')),NOW()) as no_of_days from employee
union
	select id, 
	datediff(DATE_FORMAT(date_of_birth,concat('%',(YEAR(CURDATE())+1),'-%m-%d')),NOW()) as no_of_days from employee
) AS upcomingbirthday
WHERE no_of_days>0 
GROUP BY id 
ORDER BY no_of_days asc

 

Leave a Reply

Your email address will not be published. Required fields are marked *