user2881954 user2881954 - 1 year ago 62
SQL Question

Get records around a birthday

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

WHERE cbe_user_birthday - INTERVAL 10 YEARS AND
cbe_user_birthday + INTERVAL 10 YEARS

does not work.

Answer Source

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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download