4

我在 MySQL 中有一个包含开始和停止时间的表。

我想锻炼从停止到下一次开始的时间。

我用我的桌子做了一个 sqlfiddle http://sqlfiddle.com/#!2/2c043/1

ID  start_time  end_time
410 1367894268  1367898275
409 1367893164  1367894268
408 1367888257  1367893153
407 1367837969  1367888247

所以我想做的是

end_time from row 410 - the start_time from row 409
1367898275 - 1367893164 

并输出像

end_time      start_time    time_duration
1367898275    1367893164    5111

谢谢你的帮助。

4

2 回答 2

2

要实现您的要求,请尝试以下操作:

SELECT (B.start_time - A.end_time) AS time_duration
FROM MyTable A INNER JOIN MyTable B ON B.ID = (A.ID + 1)
ORDER BY A.ID ASC

如果ID不连续,那么您可以使用

SELECT (B.start_time - A.end_time) AS time_duration
FROM MyTable A CROSS JOIN MyTable B
WHERE B.ID IN (SELECT MIN (C.ID) FROM MyTable C WHERE C.ID > A.ID)
ORDER BY A.ID ASC
于 2013-05-07T04:47:47.453 回答
2

试试下面,它会帮助你

SQL FIDDLE: http ://sqlfiddle.com/#!2/2c043/26

SELECT 
A.idtrip_data, 
A.start_time,
B.end_time, 
(B.end_time - a.start_time) AS timedifference
FROM myTable A 
INNER JOIN myTable B 
ON B.idtrip_data = (A.idtrip_data + 1)
ORDER BY A.idtrip_data ASC
于 2013-05-07T05:03:53.117 回答