SELECT name, (TO_DAYS(CURDATE()) - TO_DAYS(start_date))/365 AS service_years FROM s_emp
ORDER BY service_years DESC
The above code does not take into account leap years. You will want to use the DateDiff function or the DateAdd function. The DateAdd function allows for you to pass an "interval" in as a parameter. This will allow the function to perform a subtraction routine.
Last edited by danielsonchris; 2007-11-30 at 01:16 AM.
Reason: End user wanted result in years not days.
Must read Review for Serious PHP Developers
NuSphere PhpED 5.0
: The Staff of php-editors.com recently spent a few days working with NuSphere
PhpED 5.0
(a popular PHP IDE) and
NuCoder 1.4
(a PHP Encoding Utility), read up on all the details.