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

后端 未结 13 1551
鱼传尺愫
鱼传尺愫 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-29 07:40

    Assumed the given date is greater than the current date,

    1.Find the total no of days b/w the current date and the given date.

    -> DATEDIFF(NOW(),'1988-05-01')

    2.Find the no of years from the calculated no of days.

    -> DATEDIFF(NOW(),'1988-05-01')/365.25

    3.The age should be the completed no of years by a person. To get it, we can use 'floor' to the calculated no of years.

    -> FLOOR(DATEDIFF(NOW(),'1988-05-01')/365.25)

    Example: SELECT FLOOR(DATEDIFF(NOW(),'1988-05-01')/365.25) AS age;

提交回复
热议问题