从另一行中减去mysql行

时间:2016-10-17 13:26:36

标签: mysql timestamp subquery

以下是我的表格内容,

+---------------------+
| 2016-10-13 00:02:28 |
| 2016-10-13 00:12:27 |
| 2016-10-13 00:22:20 |
| 2016-10-13 00:12:05 |
| 2016-10-13 00:32:25 |
| 2016-10-13 00:34:05 |
| 2016-10-13 00:33:03 |
| 2016-10-13 00:45:06 |
| 2016-10-13 00:56:14 |
| 2016-10-13 00:42:22 |

如何用1st减去第2行,第4行减去第4行?基本上我想计算这些行之间的时间间隔。

2 个答案:

答案 0 :(得分:1)

这可能会有所帮助:

match

答案 1 :(得分:0)

SELECT a.datetime, a.id, ISNULL(b.datetime, 0) - a.datetime FROM YourTable AS a LEFT JOIN YourTable AS b ON a.id = (SELECT MIN(id) FROM YourTable WHERE id > a.id)