如何比较第一个日期和最后一个日期?

时间:2014-04-03 15:16:48

标签: mysql sql mysql-workbench

我有这张桌子:

|NAME(Key)       |     LOGIN_DATE      |
|----------------|---------------------|
|mark            | 2012-10-10 10:35:00 |
|mark            | 2012-10-10 10:40:00 |
|mark            | 2012-10-10 10:45:00 |

我想知道如何找到最后和第一个日期之间的差异:

|NAME            |     TIME_DIFF       |
|----------------|---------------------|
|mark            | 10 minutes          |

我该怎么做?

3 个答案:

答案 0 :(得分:4)

select 
    name, 
    concat( timestampdiff( minute,
                           min( login_date ), 
                           max( login_date ) ),
            ' minutes' 
    ) as 'login duration'
from my_table
group by name

答案 1 :(得分:2)

SELECT name
     , SEC_TO_TIME( MAX(TIME_TO_SEC(login_date))
                  - MIN(TIME_TO_SEC(login_date))
                  ) x 
  FROM my_table 
 GROUP 
    BY name;

答案 2 :(得分:0)

SELECT MIN(`LOGIN_DATE`), MAX(`LOGIN_DATE`) WHERE `NAME(Key)` = 'mark';