Search code examples
mysqldatabaseprocedures

MySQL date calculation and selecting greater than 55


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 ;

Solution

  • 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;