查询MySql数据库,查看当前日期后30天内“生日”日期字段的条目

时间:2013-05-11 21:36:27

标签: php mysql date-of-birth

我正在尝试编写一个查询MySql数据库的PHP脚本,并返回生日日期字段在接下来的30天内的条目。我能够这样做,但我需要在生成查询时从生日日期中排除年份。例如,如果今天的日期是2013年7月10日,并且有问题的生日是7/13/1991,显然这两个日期相隔超过30天,但如果我们排除年份,它们相隔仅3天。让这个生日有效。

Birthday字段在MySql数据库中存储为DATE。

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

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

如何修改此查询以不考虑年份?

4 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

答案 2 :(得分: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。不幸的是,我没有找到更好的方法,只设置DATETIME的年份部分。

答案 3 :(得分:-1)

我没有对它进行过测试,但我认为这样做会有所体现,而且从阅读中可以清楚地了解它的作用。

SELECT * FROM Club WHERE DAYOFYEAR(Birthday) BETWEEN DAYOFYEAR(NOW()) AND DAYOFYEAR(NOW()) + 30;