Sort upcoming birthdays based on current date

后端 未结 5 952
庸人自扰
庸人自扰 2020-12-06 08:01

I have the following table of people and their birthdays:

name        birthday
----------------------
yannis      1979-06-29
natalia     1980-08-19
kostas            


        
5条回答
  •  醉话见心
    2020-12-06 08:23

    Not pretty, but works

    SELECT * 
    ,CASE WHEN BirthdayThisYear>=NOW() THEN BirthdayThisYear ELSE BirthdayThisYear + INTERVAL 1 YEAR END AS NextBirthday
    FROM (
        SELECT * 
        ,birthday - INTERVAL YEAR(birthday) YEAR + INTERVAL YEAR(NOW()) YEAR AS BirthdayThisYear
        FROM bd
    ) AS bdv
    ORDER BY NextBirthday
    

提交回复
热议问题