user2881954 user2881954 - 4 months ago 7
SQL Question

Get records around a birthday

I would like to get all records plus/minus 10 years around the user birthday.

SELECT * FROM users
WHERE cbe_user_birthday - INTERVAL 10 YEARS AND
cbe_user_birthday + INTERVAL 10 YEARS


does not work.

Answer

Just use BETWEEN and some date functions. For instance, if the user's birthday is on 2001-01-01, you could do:

SELECT * FROM users WHERE cbe_user_birthday BETWEEN
    DATE_SUB('2001-01-01', INTERVAL 10 YEAR) AND
    DATE_ADD('2001-01-01', INTERVAL 10 YEAR)