tags:

views:

29

answers:

1

My MySQL database has date of birth stored in the format yyyy-mm-dd, how do I query the database - without using php code - to show me results of people born within x years of now, and/or, who are y years of age?

The table is called users, the colum is called dob. I've spent a while viewing the MySQL manual but I can't work out precisely how to form my query.

So far I went for select * from users where dob... but I don't think that's the way to go.

+1  A: 

Use the INTERVAL command.

SELECT * from users where dob >= (CURDATE() - INTERVAL 18 YEAR)

This returns all users under the age of 18.

David
Thanks! I was playing with the interval but couldn't get it to do what I wanted, saved me hours of fruitlessness. Just want to add that I had to remove the S from YEARS to make it YEAR, more for in case someone views this question in the future. Again, many thanks!
Confused
Thanks for the tip, I couldn't remember it offhand. I'll edit in the fix.
David