I have a table of users with a field birthday(DATE).
Currently, I select users who’s birthdays is within 7 days past or ahead of the current date like this:
SELECT * FROM users
WHERE
DATE_FORMAT(birthday, '1980-%m-%d')
BETWEEN
DATE_FORMAT(DATE_SUB(CURRENT_DATE(), INTERVAL 7 day), '1980-%m-%d')
AND
DATE_FORMAT(DATE_ADD(CURRENT_DATE(), INTERVAL 7 day), '1980-%m-%d')
As you can see, this query is not the most streamlined, considering the comparison via the fabricated 1980- date formatting. Also, I believe this will fail to properly select birthday users around the last and first week of the year.
While I am seeing the problems with this, I struggle to find how to approach this. What could I do to improve this query?
Try this filter –