Assuming the table is named people
:
SELECT * FROM people ORDER BY DATEDIFF( death, birth ) DESC
For a more precise calculation, use TIMESTAMPDIFF()
and specify the unit of measurement:
SELECT * FROM people ORDER BY TIMESTAMPDIFF( MINUTE, birth, death ) DESC
Here the later date is supplied last, and the earlier date first (backward from DATEDIFF()
).
The DATEDIFF()
function calculates the number of days between parameter 1 minus parameter 2. By ordering DESC (descending) we are instructing MySQL to return the largest # of days first.
Here is the MySQL reference on the DATEDIFF()
function: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_datediff
And for TIMESTAMPDIFF()
: https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_timediff