我正在尝试设置一个查询,该查询将拉出今天生日的所有客户。我正在使用phpmyadmin
localhost
via Unix socket
,生日设置为DATE
.
我只需要返回firstname (emp.emp_firstname as
名字)
、lastname (emp.emp_lastname as
姓氏)
和date of birth (emp_birthday as 'dob')
我正在尝试设置一个查询,该查询将拉出今天生日的所有客户。我正在使用phpmyadmin
localhost
via Unix socket
,生日设置为DATE
.
我只需要返回firstname (emp.emp_firstname as
名字)
、lastname (emp.emp_lastname as
姓氏)
和date of birth (emp_birthday as 'dob')
一个简单的解决方案是这样的:
SELECT
emp_firstname AS first_name,
emp_lastname AS last_name,
emp_birthday AS dob
FROM
emp
WHERE
MONTH(emp_birthday)=MONTH(CURDATE())
AND DAY(emp_birthday)=DAY(CURDATE())
或者如果你想考虑闰年,你可以使用这个:
SELECT
emp_firstname AS first_name,
emp_lastname AS last_name,
emp_birthday AS dob
FROM
emp
WHERE
emp_birthday +
INTERVAL
YEAR(CURDATE())-YEAR(emp_birthday) +
(MONTH(emp_birthday)<MONTH(CURDATE())
OR (MONTH(emp_birthday)=MONTH(CURDATE()) AND DAY(emp_birthday)<DAY(CURDATE())))
YEAR = CURDATE()
如果某人的出生日期是 2 月 29 日,而今天是 2 月 28 日,并且今年不是闰年,我的最后一个查询会将他/她的生日视为今天。
我的第二个查询也可以这样简化:
SELECT
emp_firstname AS first_name,
emp_lastname AS last_name,
emp_birthday AS dob
FROM
emp
WHERE
(MONTH(emp_birthday)=MONTH(CURDATE())
AND DAY(emp_birthday)=DAY(CURDATE()))
OR (DAY(LAST_DAY(emp_birthday))=29
AND DAY(emp_birthday)=29
AND DAY(LAST_DAY(CURDATE()))=28);
select emp_firstname as first_name,
emp_lastname as last_name,
emp_birthday as dob
from emp
where month(emp_birthday) = month(curdate())
and day(emp_birthday) = day(curdate())
select emp_firstname as first_name,
emp_lastname as last_name,
emp_birthday as dob
from emp
where DATE_FORMAT(emp_birthday,'%m-%d') = DATE_FORMAT(NOW(),'%m-%d')
对不起,但为什么不只是
SELECT
emp_firstname AS first_name,
emp_lastname AS last_name,
emp_birthday AS dob
FROM rmp
WHERE DATE(emp_birthday) = CURDATE();
?
您可能会遇到日期匹配问题。Curdate() 返回一个午夜时间戳,但由于难以猜测的原因,您的出生日期本身可能不是午夜时间戳。尝试这个:
SELECT emp_firstname AS first name,
emp_lastname AS last name,
emp_birthday AS dob
FROM emp
WHERE emp_birthday >= curdate()
AND emp_birthday < curdate() + INTERVAL 1 DAY