1

我正在尝试设置一个查询,该查询将拉出今天生日的所有客户。我正在使用phpmyadmin localhostvia Unix socket,生日设置为DATE.

我只需要返回firstname (emp.emp_firstname as名字)lastname (emp.emp_lastname as姓氏)date of birth (emp_birthday as 'dob')

4

5 回答 5

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);
于 2013-06-28T11:45:41.153 回答
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())

SQLFiddle 演示

于 2013-06-28T11:28:49.157 回答
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')
于 2013-06-28T12:22:56.043 回答
0

对不起,但为什么不只是

SELECT 
    emp_firstname AS first_name, 
    emp_lastname AS last_name, 
    emp_birthday AS dob
FROM rmp
WHERE DATE(emp_birthday) = CURDATE();

?

于 2013-06-28T13:12:33.030 回答
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
于 2013-06-28T11:46:52.203 回答