0

I have this situation where I have date-of-birth stored in database eg. 1990/03/15. I want to find out info. of records whose age is between 20 & 30. How should I write this query?

I have tried this: I get age of all candidates using this query:

SELECT TIMESTAMPDIFF(YEAR,dob,CURDATE()) as age FROM `profile`

How to use it to get list of all candidates between age 20 & 30?

Nevermore
  • 882
  • 2
  • 18
  • 44

1 Answers1

2

The NOW() gives the current Date. Now, to get date 20 and 30 years older, we can do the following:

DATE_SUB(NOW(), INTERVAL 20 YEAR)
AND 
DATE_SUB(date, INTERVAL 30 YEAR)

Now the remaining query is very simple. That is,

SELECT * FROM Person 
WHERE dateOfBirth BETWEEN DATE_SUB(NOW(), INTERVAL 20 YEAR) 
                  AND DATE_SUB(date, INTERVAL 30 YEAR)
Aman Jain
  • 655
  • 5
  • 17