SELECT * users who are a certain age

People who are between 18 and 19 years old encompass a range of birthdates over 2 years. Your query only covers 1 year. Consider reworking your logic.


I would advise something like:

SELECT *
FROM `users`
WHERE dob BETWEEN 
    CURDATE() - INTERVAL 18 YEAR 
    AND CURDATE() - INTERVAL 20 YEAR 

Technically that will include people who turn 20 today. You can change the interval to a DATE_ADD and be more precise if you'd like.

This is a bit cleaner, because you don't need to rework the two birthdays in PHP (or god forbid, manually) each time.


I don't have enough reputation to comment, but David Grenier's answer will work if you put the greater interval first (and YEAR not YEARS, as pointed out)

SELECT *
FROM `users`
WHERE dob BETWEEN 
CURDATE() - INTERVAL 20 YEAR 
AND CURDATE() - INTERVAL 18 YEAR 

Tags:

Mysql

Sql