1

以下查询返回 start_time 和 end_time 中的时间戳,以及每个时间戳之间的分钟差(以秒/60 为单位)。我还计算前 15 天,因为总数需要是“滚动总数”。每天有多个时间戳。

我想要做的是将每天的总“时间差异”相加,然后按以下方式将天组合在一起:

10-08-2013 - 小时:分钟:秒

10-09-2013 - 小时:分钟:秒

SELECT start_time, end_time, 
SUM(TIMESTAMPDIFF(second, start_time, end_time) /60) as `Time Diff`
FROM time
WHERE start_time >= DATE_SUB(NOW(), INTERVAL 15 DAY)
AND user_id = 'xx'
GROUP BY start_time, end_time
4

1 回答 1

1

如果您有列日期,则不需要GROUP BY start_time, end_time创建(我建议您创建列日期以对“时间差异”进行分组)。
这是我的例子:
我的表(命名时间)

++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    date   |     starttime       |       endtime       |
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2013-10-23 | 2013-10-23 08:00:00 | 2013-10-23 16:30:00 |
2013-10-24 | 2013-10-24 08:30:00 | 2013-10-24 17:00:00 |

这是我的查询以显示开始时间和结束时间之间的不同时间:

SELECT *, TIMEDIFF(endtime,starttime) AS duration FROM time

它会返回:

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    date   |     starttime       |       endtime       | duration |
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2013-10-23 | 2013-10-23 08:00:00 | 2013-10-23 16:30:00 | 08:30:00 |
2013-10-24 | 2013-10-24 08:30:00 | 2013-10-24 17:00:00 | 08:30:00 |

那是如果您有一个date与开始时间和结束时间不同的列。
你没有给我你表格的结构,所以我看不清楚你的问题。

更新:
我想你有一个这样的表: 表数据
可能你的问题是:calculate the time between starting time and ending time from a day of a user that the user could start and stop in anytime (at that day)
我运行这个查询来做到这一点:

SELECT *, TIMEDIFF(MAX(end),MIN(start)) AS duration FROM time
GROUP BY user_id, date 
ORDER BY date ASC;

它将返回:
结果 1
或者如果您运行此查询:

SELECT 
user_id,
MIN(start) AS start, 
MAX(end) AS end, 
TIMEDIFF(MAX(end),MIN(start)) AS duration 
FROM time
GROUP BY user_id, date 
ORDER BY date ASC

它会返回这个:
结果 2

于 2013-10-23T04:26:31.113 回答