0

假设我有下表:

name        birth(timestamp)          death(timestamp)
--------   ----------------------   ---------------------
person1      22-05-2013 21:59:00     22-05-2013 21:59:00
person2      20-05-2013 21:58:58     27-06-2025 21:59:55

如何以首先返回寿命最长的人的方式对该表进行排序?

4

1 回答 1

0

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

于 2013-05-22T19:03:18.490 回答