gpt4 book ai didi

MySQL获取位于给定条件之间的逗号分隔的记录ID行

转载 作者:行者123 更新时间:2023-11-30 23:52:17 25 4
gpt4 key购买 nike

我很难建立逻辑。我有一个 MySQL 表,其中的数据或多或少类似于以下内容。数据按日期字段排序。

ID      Code  Type  Date 
4 PQR 0 2019-10-17 22:30:00
7 PQR 0 2019-10-17 21:30:00
3 XYZ 0 2019-10-17 20:30:00
1 ABC 1 2019-10-17 19:30:00
5 PQR 0 2019-10-17 18:30:00
6 PQR 0 2019-10-17 17:30:00
2 PQR 0 2019-10-17 16:30:00
8 PQR 0 2019-10-17 15:30:00
9 XYZ 0 2019-10-17 14:30:00
14 ABC 0 2019-10-17 13:30:00
11 XYZ 0 2019-10-17 12:30:00
12 RPM 2 2019-10-17 11:30:00
13 PQR 0 2019-10-17 10:30:00
10 PQR 0 2019-10-17 09:30:00
15 PQR 0 2019-10-17 08:30:00
18 XYZ 0 2019-10-17 07:30:00
17 ABC 1 2019-10-17 06:30:00
16 XYZ 0 2019-10-17 05:30:00
19 XYZ 0 2019-10-17 05:00:00
24 XYZ 0 2019-10-17 04:45:00
21 PQR 0 2019-10-17 04:30:00
26 PQR 0 2019-10-17 04:00:00
23 PQR 0 2019-10-17 03:45:00
20 PQR 0 2019-10-17 03:30:00
25 PQR 0 2019-10-17 03:15:00
22 ABC 0 2019-10-17 03:00:00

我想完成的是——

  1. 在代码=ABC、类型=0 和代码=ABC、类型=1 的行之间获取代码=PQR 的所有行的逗号分隔 ID
  2. 所以结果看起来像 -
| FromID | ToCode |       PQRIds      |
+--------+--------+-------------------+
| 22 | 17 | 21,26,23,20,25 |
+--------+--------+-------------------+
| 17 | 14 | 13,10,15 |
+--------+--------+-------------------+
| 14 | 1 | 5,6,2,8 |

更新

-22-ABC-0和17-ABC-1之间PQR很少

-17-ABC-1和14-ABC-0之间PQR很少

-14-ABC-0和1-ABC-1之间PQR很少

最佳答案

1) 为每个abc分配一个 block 号和行号 2) 聚合 3) 通过匹配行号找到id

drop table if exists t;
create table t
(ID int, Code varchar(3), Type int,dt datetime);
insert into t (id,code,type,dt) values
(4 , 'PQR' , 0 , '2019-10-17 22:30:00'),
(7 , 'PQR' , 0 , '2019-10-17 21:30:00'),
(3 , 'XYZ' , 0 , '2019-10-17 20:30:00'),
(1 , 'ABC' , 1 , '2019-10-17 19:30:00'),
(5 , 'PQR' , 0 , '2019-10-17 18:30:00'),
(6 , 'PQR' , 0 , '2019-10-17 17:30:00'),
(2 , 'PQR' , 0 , '2019-10-17 16:30:00'),
(8 , 'PQR' , 0 , '2019-10-17 15:30:00'),
(9 , 'XYZ' , 0 , '2019-10-17 14:30:00'),
(14 , 'ABC' , 0 , '2019-10-17 13:30:00'),
(11 , 'XYZ' , 0 , '2019-10-17 12:30:00'),
(12 , 'RPM' , 2 , '2019-10-17 11:30:00'),
(13 , 'PQR' , 0 , '2019-10-17 10:30:00'),
(10 , 'PQR' , 0 , '2019-10-17 09:30:00'),
(15 , 'PQR' , 0 , '2019-10-17 08:30:00'),
(18 , 'XYZ' , 0 , '2019-10-17 07:30:00'),
(17 , 'ABC' , 1 , '2019-10-17 06:30:00'),
(16 , 'XYZ' , 0 , '2019-10-17 05:30:00'),
(19 , 'XYZ' , 0 , '2019-10-17 05:00:00'),
(24 , 'XYZ' , 0 , '2019-10-17 04:45:00'),
(21 , 'PQR' , 0 , '2019-10-17 04:30:00'),
(26 , 'PQR' , 0 , '2019-10-17 04:00:00'),
(23 , 'PQR' , 0 , '2019-10-17 03:45:00'),
(20 , 'PQR' , 0 , '2019-10-17 03:30:00'),
(25 , 'PQR' , 0 , '2019-10-17 03:15:00'),
(22 , 'ABC' , 0 , '2019-10-17 03:00:00');

Select fromid,toid,gc from
(
select bn,group_concat(case when code = 'pqr' then id end order by dt desc) gc,
min(rownumber) minrownumber, max(rownumber) + 1 maxrownumber
from
(
select t.*,
if(code = 'abc', @bn:=@bn+1,@bn:=@bn) bn,
@rn:=@rn+1 rownumber,
@p:=code p
from t
cross join (select @bn:=0,@rn:=0,@p:=0) r
order by dt
) s
group by bn
) t1
join
(select t.id fromid,t.code fromcode,
@rn1:=@rn1+1 rownumber
from t
cross join (select @rn1:=0) r
order by dt
) t2
on t2.rownumber = t1.minrownumber
join
(select t.id toid,t.code tocode,
@rn2:=@rn2+1 rownumber
from t
cross join (select @rn2:=0) r
order by dt
) t3
on t3.rownumber = t1.maxrownumber;

+--------+------+----------------+
| fromid | toid | gc |
+--------+------+----------------+
| 22 | 17 | 21,26,23,20,25 |
| 17 | 14 | 13,10,15 |
| 14 | 1 | 5,6,2,8 |
+--------+------+----------------+
3 rows in set (0.12 sec)

关于MySQL获取位于给定条件之间的逗号分隔的记录ID行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58796890/

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