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

前端 未结 7 1628
梦谈多话
梦谈多话 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条回答
  •  猫巷女王i
    2021-01-05 23:10

    My solution is as follow:

    select cm.id from users cm where 
    date(concat(
    year(curdate()) - (year(subdate(curdate(), 14)) < year(curdate())
    and month(curdate()) < month(cm.birthday)) + (year(affffdate(curdate(), 14)) > year(curdate())
    and month(curdate()) > month(cm.birthday)), date_format(cm.birthday, '-%m-%d'))) between subdate(curdate(), 14)
    and affffdate(curdate(), 14);
    

    It looks like it works fine when the period captures the current and next year or the current and previous year

提交回复
热议问题