gpt4 book ai didi

mysql - 选择区域无人的时间 - mysql

转载 作者:行者123 更新时间:2023-11-29 11:13:11 25 4
gpt4 key购买 nike

enter image description here

我有一个表,存储了人们进入某个区域的数据,他们进去的时间和他们出去的时间,每轮1人。
如果这些区域没有人,我该如何编写一个查询来每 5 分钟检查一次?
例如上表:

范围 10:10:00 到 10:15:00 => 10:10:00 到 10:14:58 没有客户端
范围 10:15:00 到 10:20:00 => 客户端从 10:15:00 到 10:20:00
范围 10:20:00 到 10:25:00 => 客户端从 10:20:00 到 10:25:00
范围 10:25:00 到 10:30:00 => 10:26:33 到 10:30:00 没有客户端
范围 10:30:00 到 10:35:00 => 10:30:33 到 10:33:42 没有客户端

进行智能查询确实很难

最佳答案

以下查询将返回该区域无人存在时的所有时间范围:

select vo.timegoout as empty_start, min(vi.startentry) as empty_end
from visits vo -- leave area (visit out)
join visits vi -- enter area (visit in)
on vi.startentry > vo.timegoout
where not exists (
select *
from visits v
where v.timegoout > vo.timegoout
and v.startentry <= vo.timegoout
)
group by vo.timegoout
union all
select '00:00:00', (select min(startentry) from visits)
union all
select (select max(timegoout) from visits), '23:59:59'
order by empty_start

想法是:

当有人离开该区域 ( vo.timegoout as empty_start ) 并且此时该区域没有其他人 ( not exists (...) ) 时,该区域为空。该区域是空的,直到有人进入为止 ( min(vi.startentry) as empty_end )。此外,该区域从 00:00:00 开始是空的。直到第一个访问者输入它( union all select '00:00:00', (select min(startentry) from visits) )。在最后一个访问者离开后它也是空的,直到 23:59:59 (union all select (select max(timegoout) from visits), '23:59:59')

给定以下示例数据

| startentry | timegoout |
| 10:00:00 | 10:01:00 |
| 10:10:10 | 10:11:11 |
| 10:12:12 | 10:13:13 |
| 12:33:33 | 12:55:55 |
| 12:34:56 | 12:44:44 |
| 14:31:00 | 14:33:00 |
| 14:32:00 | 14:34:00 |
| 16:00:00 | 16:10:00 |

我们将得到这个结果:

| empty_start | empty_end |
| 00:00:00 | 10:00:00 |
| 10:01:00 | 10:10:10 |
| 10:11:11 | 10:12:12 |
| 10:13:13 | 12:33:33 |
| 12:55:55 | 14:31:00 |
| 14:34:00 | 16:00:00 |
| 16:10:00 | 23:59:59 |

如果您想精确到一秒,则查询需要进行一些修改:

select addtime(vo.timegoout, '00:00:01') as empty_start,
subtime(min(vi.startentry), '00:00:01') as empty_end
from visits vo -- leave area (visit out)
join visits vi -- enter area (visit in)
on vi.startentry > vo.timegoout
where not exists (
select *
from visits v
where v.timegoout > vo.timegoout
and v.startentry <= vo.timegoout
)
group by vo.timegoout
union all
select '00:00:00' as empty_start, (select subtime(min(startentry), '00:00:01') from visits) as empty_end from (select 1) dummy
having empty_start <= empty_end
union all
select (select addtime(max(timegoout), '00:00:01') from visits) as empty_start, '23:59:59' as empty_end from (select 1) dummy
having empty_start <= empty_end
order by empty_start

结果:

| empty_start | empty_end |
| 00:00:00 | 09:59:59 |
| 10:01:01 | 10:10:09 |
| 10:11:12 | 10:12:11 |
| 10:13:14 | 12:33:32 |
| 12:55:56 | 14:30:59 |
| 14:34:01 | 15:59:59 |
| 16:10:01 | 23:59:59 |

要处理 5 分钟范围,您应该创建一个包含所有该范围的 halper 表:

drop table if exists tmp_sequence;
create table tmp_sequence (seq mediumint unsigned null);

insert into tmp_sequence(seq)
values (null),(null),(null),(null),(null),(null),(null),(null),(null),(null);

insert into tmp_sequence(seq) select seq from tmp_sequence;
insert into tmp_sequence(seq) select seq from tmp_sequence;
insert into tmp_sequence(seq) select seq from tmp_sequence;
insert into tmp_sequence(seq) select seq from tmp_sequence;
insert into tmp_sequence(seq) select seq from tmp_sequence;

ALTER TABLE `tmp_sequence`
CHANGE COLUMN `seq` `seq` MEDIUMINT(8) UNSIGNED NOT NULL AUTO_INCREMENT FIRST,
ADD PRIMARY KEY (`seq`);

drop table if exists helper_5_minutes_ranges;
create table helper_5_minutes_ranges(
range_start time,
range_end time,
primary key (range_start, range_end)
);
insert into helper_5_minutes_ranges (range_start, range_end)
select sec_to_time((seq-1)*5*60) as range_start, sec_to_time((seq)*5*60-1) as range_end
from tmp_sequence ts
where ts.seq <= 288
;

drop table if exists tmp_sequence;

helper_5_minutes_ranges中的数据表:

| range_start | range_end |
| 00:00:00 | 00:04:59 |
| 00:05:00 | 00:09:59 |
...
| 23:50:00 | 23:54:59 |
| 23:55:00 | 23:59:59 |

现在您可以查询以下内容:选择无人在该区域时的所有 5 分钟范围

select r.*
from helper_5_minutes_ranges r
left join visits v
on v.startentry <= r.range_end
and v.timegoout >= r.range_start
where v.startentry is null

结果:

| range_start | range_end |
| 00:00:00 | 00:04:59 |
...
| 10:05:00 | 10:09:59 |
| 10:15:00 | 10:19:59 |
...
| 12:25:00 | 12:29:59 |
| 13:00:00 | 13:04:59 |
...
| 14:25:00 | 14:29:59 |
| 14:35:00 | 14:39:59 |
...
| 15:55:00 | 15:59:59 |
| 16:15:00 | 16:19:59 |
...
| 23:55:00 | 23:59:59 |

选择有人曾在该区域时的所有 5 分钟范围

select r.*
from helper_5_minutes_ranges r
join visits v
on v.startentry <= r.range_end
and v.timegoout >= r.range_start

最后这个怪物,它将返回类似于您预期的结果:

select r.*, 'visited' as type,
greatest(v.startentry, r.range_start) as vr_start,
least(v.timegoout, r.range_end) as vr_end
from helper_5_minutes_ranges r
join visits v
on v.startentry <= r.range_end
and v.timegoout >= r.range_start

union all

select mr.*, 'empty' as type,
greatest(er.empty_start, mr.range_start) as vr_start,
least(er.empty_end, mr.range_end) as vr_end
from (
select addtime(vo.timegoout, '00:00:01') as empty_start,
subtime(min(vi.startentry), '00:00:01') as empty_end
from visits vo -- leave area (visit out)
join visits vi -- enter area (visit in)
on vi.startentry > vo.timegoout
where not exists (
select *
from visits v
where v.timegoout > vo.timegoout
and v.startentry <= vo.timegoout
)
group by vo.timegoout
union all
select '00:00:00' as empty_start, (select subtime(min(startentry), '00:00:01') from visits) as empty_end from (select 1) dummy
having empty_start <= empty_end
union all
select (select addtime(max(timegoout), '00:00:01') from visits) as empty_start, '23:59:59' as empty_end from (select 1) dummy
having empty_start <= empty_end
) er
join helper_5_minutes_ranges mr
on mr.range_start <= er.empty_end
and mr.range_end >= er.empty_start

order by range_start, vr_start

结果:

| range_start | range_end | type    | vr_start       | vr_end          |
...
| 09:55:00 | 09:59:59 | empty | 09:55:00.000000 | 09:59:59.000000 |
| 10:00:00 | 10:04:59 | visited | 10:00:00.000000 | 10:01:00.000000 |
| 10:00:00 | 10:04:59 | empty | 10:01:01.000000 | 10:04:59.000000 |
| 10:05:00 | 10:09:59 | empty | 10:05:00.000000 | 10:09:59.000000 |
| 10:10:00 | 10:14:59 | empty | 10:10:00.000000 | 10:10:09.000000 |
| 10:10:00 | 10:14:59 | visited | 10:10:10.000000 | 10:11:11.000000 |
| 10:10:00 | 10:14:59 | empty | 10:11:12.000000 | 10:12:11.000000 |
| 10:10:00 | 10:14:59 | visited | 10:12:12.000000 | 10:13:13.000000 |
| 10:10:00 | 10:14:59 | empty | 10:13:14.000000 | 10:14:59.000000 |
...

关于mysql - 选择区域无人的时间 - mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40191111/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com