我正在尝试设置一个查询,以便拉出今天生日的所有客户。我通过phpmyadmin
使用localhost
Unix socket
,生日设置为DATE
。
我需要返回的是firstname (emp.emp_firstname as
名字)
,lastname (emp.emp_lastname as
姓氏)
和date of birth (emp_birthday as 'dob')
答案 0 :(得分:5)
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())
答案 1 :(得分:5)
一个简单的解决方案就是这样:
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);
答案 2 :(得分:1)
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')
答案 3 :(得分:0)
您可能遇到日期匹配问题。 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
答案 4 :(得分:0)
抱歉,为什么不呢
SELECT
emp_firstname AS first_name,
emp_lastname AS last_name,
emp_birthday AS dob
FROM rmp
WHERE DATE(emp_birthday) = CURDATE();