SQL that list all birthdays within the next and previous 14 days

前端 未结 7 1487
梦谈多话
梦谈多话 2021-01-05 22:30

I have a MySQL member table, with a DOB field which stores all members\' dates of birth in DATE format (Notice: it has the \"Year\" pa

7条回答
  •  清酒与你
    2021-01-05 22:50

    Here's the simplest code to get the upcoming birthdays for the next x days and previous x days

    this query is also not affected by leap-years

    SELECT name, date_of_birty 
    FROM users 
    WHERE DATE(CONCAT(YEAR(CURDATE()), RIGHT(date_of_birty, 6)))
              BETWEEN 
                  DATE_SUB(CURDATE(), INTERVAL 14 DAY)
              AND
                  DATE_ADD(CURDATE(), INTERVAL 14 DAY)
    

提交回复
热议问题