[英]MYSQL: Get Upcoming Birthdays Year End Issue
Currently, I have a MySQL query to return upcoming birthdays from the current date to the next 7 days, and it is working fine, but since it's the end of the year I have issues getting results.目前,我有一个 MySQL 查询,用于返回从当前日期到接下来 7 天的即将到来的生日,它工作正常,但由于是年底,我在获取结果时遇到了问题。
The main issue is after December 25th it checks birthdays between 12-26 and 01-02 due to the end of the year.主要问题是在 12 月 25 日之后,由于年底,它会检查 12-26 和 01-02 之间的生日。 Below is my code
下面是我的代码
SELECT u.id, u.birthday FROM users as u
WHERE DATE_FORMAT(u.birthday, '%m-%d') >= DATE_FORMAT(NOW(), '%m-%d') and DATE_FORMAT(u.birthday, '%m-%d') <= DATE_FORMAT((NOW() + INTERVAL +7 DAY), '%m-%d');
Can someone help me to update this query to get results?有人可以帮我更新此查询以获得结果吗?
I tried to add the year-to-date format but it doesn't give expected results.我试图添加年初至今的格式,但它没有给出预期的结果。
You need to check if the year in a week is different from the current year, and use a different condition.您需要检查一周中的年份是否与当前年份不同,并使用不同的条件。 The condition then should be
OR
rather than AND
.那么条件应该是
OR
而不是AND
。
WHERE CASE WHEN YEAR(NOW()) = YEAR(NOW() + INTERVAL 7 DAY)
THEN DATE_FORMAT(u.birthday, '%m-%d') BETWEEN DATE_FORMAT(NOW(), '%m-%d') AND DATE_FORMAT((NOW() + INTERVAL +7 DAY), '%m-%d')
ELSE DATE_FORMAT(u.birthday, '%m-%d') >= DATE_FORMAT(NOW(), '%m-%d') OR DATE_FORMAT(u.birthday, '%m-%d') <= DATE_FORMAT((NOW() + INTERVAL +7 DAY), '%m-%d')
END
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.