如何找到 mysql table 记录之间的时间间隔和间隔长度?
how to find time gaps and gap length between mysql table records?
我的 table 看起来像这样:
id
时间
1
2021-07-17 17:44:26
2
2021-07-17 17:44:26
3
2021-07-17 17:44:26
4
2021-07-17 17:44:31
5
2021-07-17 17:44:31
6
2021-07-17 17:44:31
7
2021-07-17 17:44:36
8
2021-07-17 17:44:36
9
2021-07-17 17:44:36
10
2021-07-17 17:44:41
11
2021-07-17 17:44:41
12
2021-07-17 17:44:41
13
2021-07-1717:44:51
14
2021-07-1717:44:51
15
2021-07-1717:44:51
16
2021-07-1717:44:56
17
2021-07-1717:44:56
18
2021-07-1717:44:56
19
2021-07-17 17:45:02
20
2021-07-17 17:45:02
21
2021-07-17 17:45:02
我有 MySQL 8.0.21
总是接下来的 3 行有相同的时间,然后 beetwen 通常是 5 秒的时间间隔,如何找到所有超过 8 秒的间隔并计算间隔时间以获得类似的东西:
gap_id
gap_time_start
gap_length
1
2021-07-17 17:44:41
10
如果您使用的是 MySQL 8+,您可以像这样使用 LEAD()
window 函数:
select * from (
SELECT id as gap_id,
`time` as gap_start_time,
timediff( lead(`time`) over W, `time`) as gap_length
from `myTable` window w as (order by `time` asc)
) T where T.gap_length > 5;
输出:
12, 2021-07-17 17:44:41, 00:00:10
18, 2021-07-17 17:44:56, 00:00:06
参考:LEAD()
我的 table 看起来像这样:
id | 时间 |
---|---|
1 | 2021-07-17 17:44:26 |
2 | 2021-07-17 17:44:26 |
3 | 2021-07-17 17:44:26 |
4 | 2021-07-17 17:44:31 |
5 | 2021-07-17 17:44:31 |
6 | 2021-07-17 17:44:31 |
7 | 2021-07-17 17:44:36 |
8 | 2021-07-17 17:44:36 |
9 | 2021-07-17 17:44:36 |
10 | 2021-07-17 17:44:41 |
11 | 2021-07-17 17:44:41 |
12 | 2021-07-17 17:44:41 |
13 | 2021-07-1717:44:51 |
14 | 2021-07-1717:44:51 |
15 | 2021-07-1717:44:51 |
16 | 2021-07-1717:44:56 |
17 | 2021-07-1717:44:56 |
18 | 2021-07-1717:44:56 |
19 | 2021-07-17 17:45:02 |
20 | 2021-07-17 17:45:02 |
21 | 2021-07-17 17:45:02 |
我有 MySQL 8.0.21
总是接下来的 3 行有相同的时间,然后 beetwen 通常是 5 秒的时间间隔,如何找到所有超过 8 秒的间隔并计算间隔时间以获得类似的东西:
gap_id | gap_time_start | gap_length |
---|---|---|
1 | 2021-07-17 17:44:41 | 10 |
如果您使用的是 MySQL 8+,您可以像这样使用 LEAD()
window 函数:
select * from (
SELECT id as gap_id,
`time` as gap_start_time,
timediff( lead(`time`) over W, `time`) as gap_length
from `myTable` window w as (order by `time` asc)
) T where T.gap_length > 5;
输出:
12, 2021-07-17 17:44:41, 00:00:10
18, 2021-07-17 17:44:56, 00:00:06
参考:LEAD()