How to get an age from a D.O.B field in MySQL?

后端 未结 13 1542
鱼传尺愫
鱼传尺愫 2020-11-29 07:01

I need to calculate the age of a \"customer\" from their date of birth.

I have tried to use the following:

DATEDIFF(year, customer.dob, \"2010

13条回答
  •  孤独总比滥情好
    2020-11-29 07:46

    This is the simplest I could come up with so far:

    SELECT FLOOR(ABS(DATEDIFF(d, CURRENT_TIMESTAMP, dob))/365.25) AS age
    

    First we get the date difference in days, then convert it to years, then FLOOR truncates to the integer part of the number.

提交回复
热议问题