Calculate age from date of birth column using mysql query

Calculate age from date of birth column using mysql query

Suppose we have a user table with dob column in mysql. And we have to calculate age of user’s on the basis of date of birth mentioned in the dob column in the user table. Then a question may arise that how can we calculate age from date of birth column using mysql query.

So here i will show you a query to calculate age where we will take out the difference between current date and dob column value and then divide it to 365. The result would be the actual age of the user. The query for this would be like below.

SELECT 
        id,

  (DATEDIFF(CURRENT_DATE, STR_TO_DATE(dob, '%Y-%m-%d'))/365) as age 

FROM `users`

Here i have used DATEDIFF function of mysql and STR_TO_DATE and using CURRENT_DATE to get the current date.