我在MySQL中有一个启动和停止时间表。
我想训练从停止到下一次开始的时间。
我用我的桌子制作了一个平方米 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
感谢您的帮助。
答案 0 :(得分: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
答案 1 :(得分: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