Meera M Babu
Meera M Babu

Reputation: 68

How to calculate years and months between more than two dates in mysql

I have a table 'work_experience' storing the experience details of candidate.

Screen shot for work_experience table

from and to columns stores the experience of a user in a company. They are date fields.
I want to get total experience of a user in years and months.

My tries are:

SELECT
    SUM(TIMESTAMPDIFF(YEAR, 'from', 'to')) AS years,
    SUM(TIMESTAMPDIFF(MONTH, 'from', 'to')) % 12 AS months
FROM
    work_experience
WHERE
    'userid' = 15;

OUTPUT: 1 years 7 months

Actually the result has to be 2years 7 months. How can I get it? Thanks in advance.

Upvotes: 2

Views: 378

Answers (1)

Meera M Babu
Meera M Babu

Reputation: 68

This is working for me.:)

SELECT FLOOR(SUM(TIMESTAMPDIFF(MONTH,from,to))/12) AS years, SUM(TIMESTAMPDIFF(MONTH,from,to))%12 AS months FROM work_experience WHERE userid=15.

Thanks to all...

Upvotes: 1

Related Questions