0

I have a database table with a date of birth column. I need a query that lists people in the 20-24 age range. At the same time, this query will be in a where clouse. I tried to use the DATEDIFF() function but I couldn't get what I wanted.

SELECT DATE_FORMAT(FROM_DAYS(DATEDIFF(NOW(),'DATE_OF_BIRTH')), '%Y');

I find out that I can use this query but I can't put it in a where clouse. Is there an easier way?

Jens
  • 67,715
  • 15
  • 98
  • 113
  • 2
    Why can't you put it in a `WHERE` clause? – Tim Biegeleisen Jan 22 '23 at 11:54
  • You can simplify your task by dividing with 365 your days, then using `FLOOR` to get the age. Check it [here](https://www.db-fiddle.com/f/5RPVxyaNb6znbW7VgXDRV8/0). – lemon Jan 22 '23 at 12:05
  • In MySQL you can use `where timestampdiff(year, date_of_birth, now()) between 20 and 24`. You are using incorrect quotes by the way. `'DATE_OF_BIRTH'` is a string consisting of the letters D, A, T, E, etc. Remove the quotes to access a column name, or if you must use quotes, use double quotes as defined in standard SQL or MySQL's propriatary backticks. – Thorsten Kettner Jan 22 '23 at 12:36

0 Answers0