数据库从两个时间戳行获取持续时间

时间:2016-09-15 07:38:26

标签: mysql sql database time

我有一张包含票据用法日志的表格。如何将连续两行分组以获得持续时间?  例如:前4行我应该

ticket_id  duration
15874      03:03:31
15874      01:10:35

数据示例 enter image description here

表结构 enter image description here

1 个答案:

答案 0 :(得分:0)

我已经创建了一个示例,结果将显示GROUP BY使用datetime和UserID的日期:

SELECT start_log.UserID, TIME(start_log.created_id), TIME(end_log.created_id), 
DATE(start_log.created_id), TIMESTAMPDIFF(
MINUTE , start_log.created_id, end_log.created_id ) AS TimeInMin
FROM sample AS start_log
INNER JOIN sample AS end_log ON (start_log.UserID = end_log.UserID
AND start_log.created_id < end_log.created_id )
GROUP BY TIME(start_log.created_id), UserID

输出我的意思是持续时间以分钟为单位:更新

mysql_output