A
A
afi132014-02-08 10:59:52
PHP
afi13, 2014-02-08 10:59:52

How to select users whose birthday is in the next week in SQL?

The users table has a birthday field in the format date (1988-02-02). You need to select users whose birthday is in the next 7 days.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Kerman, 2014-02-08
@afi13

To the previous two: not everything is as obvious as it seems. You select by date of birth, not DAY of birth.
To find the birthday, you can use the DAYOFYEAR(date) function .
Should be something like
I didn't check the request, but I hope the idea is clear.

N
nowm, 2014-02-08
@nowm

You can add a condition to the SQL query:

WHERE TIMESTAMPDIFF(
    DAY, 
    DATE_ADD(`birthday`, INTERVAL YEAR(NOW())-YEAR(`birthday`) YEAR), 
    NOW()
) BETWEEN 0 AND 7

Thanks to @Kerman for the hint - I didn’t think at all that I need to pay attention to the year in `birthday`.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question