Get difference in years between two dates in MySQL as an integer

后端 未结 9 2068
死守一世寂寞
死守一世寂寞 2020-11-30 10:59

I am trying to calculate how old is a person in a database.
Let\'s suppose to have this simple table:

student(id, birth_date);

Where

9条回答
  •  难免孤独
    2020-11-30 11:26

    I'd suggest this:

    DATE_FORMAT(NOW(),"%Y")
       -DATE_FORMAT(BirthDate,'%Y')
       -(
         IF(
          DATE_FORMAT(NOW(),"%m-%d") < DATE_FORMAT(BrthDate,'%m-%d'),
          1,
          0
         )
        ) as Age
    

    This should work with leap-years very well ;-)

提交回复
热议问题