问题
Ok, I have the following Users table:
id | name | birthday
1 | nam1 | 1980-06-29
2 | nam2 | 1997-07-08
3 | nam3 | 1997-07-20
Assuming that today is 2012-06-29
How can I get the users who will celebrate your birthdays in the next 15 days? I have tried with this:
select * from users where birthday between '%-06-29' and '%-07-14';
but looks like is not a valid query.
回答1:
I think the safest way is to compare their age in 15 days with their age today:
SELECT *
FROM Users
WHERE TIMESTAMPDIFF(YEAR, birthday, CURRENT_DATE + INTERVAL 15 DAY)
> TIMESTAMPDIFF(YEAR, birthday, CURRENT_DATE)
See it on sqlfiddle.
回答2:
you should try operating on a substring of "birthday" so you can "cut" the year out of the field
来源:https://stackoverflow.com/questions/11265610/using-between-mysql-function-ignoring-year