如果您有列日期,则不需要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;
它将返回:
或者如果您运行此查询:
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
它会返回这个: