mySQL SELECT upcoming birthdays

前端 未结 17 651
我寻月下人不归
我寻月下人不归 2020-12-03 11:17

I\'m trying to write a query to select users of a database whose birthdays are in the next 7 days.

I\'ve done a lot of research but I can\'t come up with a working s

17条回答
  •  一向
    一向 (楼主)
    2020-12-03 11:58

    This is what I did when I faced the same problem :

    select * from users 
    where ( month(date_of_birth) > month(CURDATE()) 
     and month(date_of_birth) < month(ADDDATE(CURDATE(),30)) ) 
     or ( month(CURDATE()) = month(date_of_birth) and day(date_of_birth) >= day(CURDATE()) 
     or month(ADDDATE(CURDATE(),30)) = month(date_of_birth) and day(date_of_birth) <= day(ADDDATE(CURDATE(),30)) ) 
     or ( year(CURDATE()) > year(ADDDATE(CURDATE(),30)) and month(date_of_birth) < month(CURDATE()) and month(date_of_birth) > month(ADDDATE(CURDATE(),30)) )
    

提交回复
热议问题