MySQL date calculation and selecting greater than 55

69 views Asked by At

I have calculated age from date of birth but I would like to select greater than 55 only in a stored procedure. Below is what I have done.

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

There are 2 answers

0
Gordon Linoff On BEST ANSWER

MySQL has an extension where you can use having for this purpose:

SELECT Firstname, Lastname, telephone_number,
       TIMESTAMPDIFF(YEAR, DOB, CURDATE())  AS age
FROM Staff
WHERE Job_title = 'instructor'
HAVING age > 55;
0
juergen d On
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 ;