sugunan sugunan - 10 months ago 67
MySQL Question

MySQL query by age range on a table which have date of birth

I have mysql table with date of birth. And I want to query the rows which based on age for current date. So following is the query for the table with date range.

SELECT * FROM `user` WHERE `dob` > '1980-01-20' AND `dob` < '1990-01-20';

How can we convert this query to search with age range for current date in the same table?

Answer Source

You could use the year function to extract the year part of the current date. So, for example, if you're looking for users between the ages of 24 and 34:

FROM   `user` 
WHERE  (YEAR(NOW()) - YEAR(`dob`)) BETWEEN 24 AND 34