3

我正在尝试编写一个 PHP 脚本来查询 MySql 数据库并返回生日日期字段在接下来的 30 天内的条目。我可以做到这一点,但我需要在查询时从生日日期中排除年份。例如,如果今天的日期是 2013 年 7 月 10 日,而有问题的生日是 1991 年 7 月 13 日,显然这两个日期相隔 30 多天,但如果我们排除年份,它们仅相隔 3 天。将此作为有效的生日返回。

生日字段作为 DATE 存储在 MySql 数据库中。

到目前为止我所做的查询:

$results = mysql_query("SELECT * FROM Club WHERE Birthday < DATE_SUB(NOW(), INTERVAL 30 DAY)");

我该怎么做才能修改此查询以不考虑年份?

4

4 回答 4

1

这是你要找的吗?

SELECT * FROM Club WHERE month(Birthday) = month(DATE_SUB(NOW(), INTERVAL 30 DAY)) and dayofmonth(Birthday) = dayofmonth(DATE_SUB(NOW(), INTERVAL 30 DAY));

http://pastebin.com/uEXrXrHT

于 2013-05-11T21:51:50.457 回答
1

设置你的窥视,确定他们的下一个生日(可以在下一个日历年),显示未来 30 天内过生日的人。在年终包装工作(12 月)等

http://sqlfiddle.com/#!2/d881b/4

create table peeps
( id int not null auto_increment,
  lastname varchar(40) not null,
birthdate datetime not null,
 primary key (id)
);

insert peeps (lastname,birthdate) values ('sam', '1973-01-01');
insert peeps (lastname,birthdate) values ('julie', '1973-02-01');
insert peeps (lastname,birthdate) values ('kim', '1973-03-01');
insert peeps (lastname,birthdate) values ('fred', '1973-04-01');
insert peeps (lastname,birthdate) values ('oscar1', '1973-05-01');
insert peeps (lastname,birthdate) values ('oscar2', '1973-05-02');
insert peeps (lastname,birthdate) values ('oscar3', '1973-05-04');
insert peeps (lastname,birthdate) values ('oscar4', '1973-05-06');
insert peeps (lastname,birthdate) values ('oscar5', '1973-05-08');
insert peeps (lastname,birthdate) values ('oscar6', '1973-05-10');
insert peeps (lastname,birthdate) values ('oscar7', '1973-05-12');
insert peeps (lastname,birthdate) values ('oscar8', '1973-05-14');
insert peeps (lastname,birthdate) values ('oscar9', '1973-05-16');
insert peeps (lastname,birthdate) values ('oscar10', '1973-05-18');
insert peeps (lastname,birthdate) values ('oscar11', '1973-05-20');
insert peeps (lastname,birthdate) values ('oscar12', '1973-05-22');
insert peeps (lastname,birthdate) values ('oscar13', '1973-05-24');
insert peeps (lastname,birthdate) values ('Felix the Cat1', '1973-06-01');
insert peeps (lastname,birthdate) values ('Felix the Cat2', '1973-06-05');
insert peeps (lastname,birthdate) values ('Felix the Cat3', '1973-06-07');
insert peeps (lastname,birthdate) values ('Bonehead7', '1973-07-01');
insert peeps (lastname,birthdate) values ('Bonehead8', '1973-08-01');
insert peeps (lastname,birthdate) values ('Bonehead9', '1973-09-01');
insert peeps (lastname,birthdate) values ('Bonehead10', '1973-10-01');
insert peeps (lastname,birthdate) values ('Bonehead11', '1973-11-01');
insert peeps (lastname,birthdate) values ('Bonehead12', '1973-12-01');

create table peeps_next_birthday
(id int not null,
 next_birthdate datetime not null
 );

insert into peeps_next_birthday (id,next_birthdate) select id,birthdate from peeps;


UPDATE peeps_next_birthday set next_birthdate=date_add(next_birthdate, interval (year(curdate())-year(next_birthdate)) year);

update peeps_next_birthday set next_birthdate=date_add(next_birthdate,interval 1 year)
where curdate()>next_birthdate;

   ///  *************************** now show the birthdays in the coming 30 days

select t2.id,t2.lastname,t1.next_birthdate
from peeps_next_birthday t1
join peeps t2
on t2.id=t1.id
where datediff(t1.next_birthdate,curdate())<=30
于 2013-05-11T23:19:16.290 回答
0

这有点棘手,但这是我的解决方案:

SELECT
    birthday,
    -- Shift the birthday into the current year
    CAST(CONCAT(YEAR(NOW()), '-', MONTH(birthday), '-', DAY(birthday)) AS DATE) AS BirthdayInCurrentYear
FROM Club
WHERE CAST(CONCAT(YEAR(NOW()), '-', MONTH(birthday), '-', DAY(birthday)) AS DATE)
    BETWEEN NOW() AND NOW() + INTERVAL 30 DAY;

此查询适用于闰年 并且日期在下一年. 您可以使用我的SQLFiddle进行测试和验证。

这可能不是最快的解决方案,因为它会创建一个 VARCHAR 并将其解析回 DATETIME。不幸的是,我没有找到更好的方法来设置日期时间的年份部分。

于 2013-05-11T23:33:08.133 回答
-1

我还没有测试过它,但我认为这会起作用,并且从阅读中可以清楚地看出它的作用。

SELECT * FROM Club WHERE DAYOFYEAR(Birthday) BETWEEN DAYOFYEAR(NOW()) AND DAYOFYEAR(NOW()) + 30;
于 2013-05-11T21:57:00.203 回答