MySQL日期计算并选择大于55

时间:2014-11-17 20:24:53

标签: mysql database procedures

我已计算出自出生日期的年龄,但我想仅在存储过程中选择大于55岁。以下是我的所作所为。

DELIMITER //
CREATE PROCEDURE p6()
BEGIN
SELECT Firstname, Lastname, telephone_number, TIMESTAMPDIFF(YEAR,DOB,CURDATE())  AS age
FROM Staff
WHERE Job_title = 'instructor';

END //
DELIMITER ;

2 个答案:

答案 0 :(得分:0)

DELIMITER //
CREATE PROCEDURE p6()
BEGIN
  SELECT Firstname, Lastname, telephone_number, 
         TIMESTAMPDIFF(YEAR,DOB,CURDATE())  AS age
  FROM Staff
  WHERE Job_title = 'instructor'
  AND TIMESTAMPDIFF(YEAR,DOB,CURDATE()) > 55;
END //
DELIMITER ;

答案 1 :(得分:0)

MySQL有一个扩展程序,您可以使用having来实现此目的:

SELECT Firstname, Lastname, telephone_number,
       TIMESTAMPDIFF(YEAR, DOB, CURDATE())  AS age
FROM Staff
WHERE Job_title = 'instructor'
HAVING age > 55;