你不需要做GROUP BY start_time, end_time
如果你有一列日期(我建議你創建列日期的Grou ps'時差')。
這裏是我的例子:
我的表(名爲時間)
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
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
列設置爲與starttime和endti不同的列我。
你沒有給我你的桌子的結構,所以我不能清楚地看到你的問題。
UPDATE:
我假設你有一個像這樣的表:
,可以是你的事是: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
它會返回此:
一位朋友幫我解答: SELECT'DATE(start_time)AS'Date',SUM(TIMESTAMPDIFF(second,start_time,end_time)/ 60)as'Time Diff' FROM WHERE start_time> = DATE_SUB(NOW(),INTERVAL 15 DAY) AND user_id ='xx' GROUP BY DATE(start_time) –