Home > Net >  Show results of employees whose age is equal to 65
Show results of employees whose age is equal to 65

Time:02-24

Show results of employees whose age is equal to 65. I want to get results whose age is 65 , i.e if any employee age comes to 65 years on 24-02-2022 his results I want to show, after 25-02-2022 his results should not display for the total 12 months, want to show for only one day. Any help

SELECT id,
      first_name,
      last_name,
      floor(datediff(CURDATE(),date_of_birth)/365) as age,
      date_of_birth 
from employee 
where floor(datediff(CURDATE(),date_of_birth)/365)=65;

Sample data

employee name - James
 
date of birth - 1957-02-24

age - 65

At this date he is 65 years and should not display after this date, i.e from 25-02-2022 to 24-02-2023

CodePudding user response:

SELECT id, 
       first_name, 
       last_name,
       TIMESTAMPDIFF(YEAR, date_of_birth, CURRENT_DATE) AS age, 
       date_of_birth 
FROM employee 
HAVING age = 65;

TIMESTAMPDIFF() returns the amount of complete (not rounded) units between two dates.


I want only exactly at the starting date when he is 65 years and 0 days.

SELECT id, 
       first_name, 
       last_name,
    -- TIMESTAMPDIFF(YEAR, date_of_birth, CURRENT_DATE) AS age, 
       65 AS age, 
       date_of_birth 
FROM employee 
WHERE date_of_birth = CURRENT_DATE - INTERVAL 65 YEAR;

https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=1a63622ffffef8167bc1be074a3a119b

  • Related