我创建了一个事件来计算生日,例如:
生日日期 = 1990-09-07 现在日期 = 2013-09-05
我的查询:
SELECT id FROM user WHERE ( birthday - NOW() ) <= 7
这个查询是错误的
我觉得DAYOFYEAR功能更适合你:
mysql> SELECT DAYOFYEAR('2000-09-07') - DAYOFYEAR(now()) AS diff;
+------+
| diff |
+------+
| 3 |
+------+
今天是2013-09-05,给了3天。现在你可以组成条件了。请注意闰年的 1 天。
你说它是生日,所以你必须考虑月份和日期,因为日期可以每个月重复..
因此,请尝试以下类似的方法,以获取年份差异。
SELECT
(YEAR(birthdate) - YEAR(NOW())) AS yeardifference
FROM
table
WHERE
MONTH(birthdate) = MONTH(NOW())
AND
DATEDIFF(birthdate, NOW()) <= 7
对于日差
SELECT
DATEDIFF(birthdate, NOW())
FROM
table
WHERE
MONTH(birthdate) = MONTH(NOW())
AND
DATEDIFF(birthdate, NOW()) <= 7
我认为您的查询应该类似于
SELECT id
FROM user
WHERE ( DAYOFYEAR(birthday) - DAYOFYEAR(NOW()) <= 7)
Dayofyear 函数会很有用,只是为了将它与您的要求联系起来
SELECT id FROM user WHERE (DAYOFYEAR(birthday) - DAYOFYEAR(NOW())) <= 7