gpt4 book ai didi

mysql - 两列之间的关系

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

在 MYSQL 中,表 A 列和 B 列中有 2 列,如果 A 列连续第 10 次出现,B 列第 11 次出现(B 可以在这 10 次之间为 1 或 0)所以我想要B的列id。

+----+---+---+
| id | A | B |
+----+---+---+
| 1 | 1 | 0 |
| 2 | 0 | 1 |
| 3 | 1 | 0 |
| 4 | 1 | 0 |
| 5 | 1 | 0 |
| 6 | 1 | 1 |
| 7 | 1 | 0 |
| 8 | 1 | 1 |
| 9 | 1 | 1 |
| 10 | 1 | 0 |
| 11 | 1 | 1 |
| 12 | 1 | 0 |
| 13 | 0 | 1 |
+----+---+---+

我需要这个(B 列 id)(其中 A 列连续出现 1(10 次)和 B 列(A 列中有争议的 10 次 1 之后的第 11 个 id)

最佳答案

在 mysql 8.0 之前的版本上,您可以在子查询中使用运行总计来帮助您解决这个问题

drop table if exists t;
create table t
(id int,A int,B int);
insert into t values
(1, 1 ,0),
(2, 0 ,1),
(3, 1 ,0),
(4, 1 ,0),
(5, 1 ,0),
(6, 1 ,1),
(7, 1 ,0),
(8, 1 ,1),
(9, 1 ,1),
(10, 1 ,0),
(11, 1 ,1),
(12, 1 ,0),
(13, 1 ,1),
(14, 1 ,1),
(15, 1 ,1),
(16, 0 ,1);

select t1.id,t1.a,t1.b
from
(
select t.*,
if(t.a = 1, @rt:=@rt+1,@rt:=0) rt
from t
cross join (select @rt:=0) r
order by t.id
) t1
where t1.rt >= 10;

+------+------+------+
| id | a | b |
+------+------+------+
| 12 | 1 | 0 |
| 13 | 1 | 1 |
| 14 | 1 | 1 |
| 15 | 1 | 1 |
+------+------+------+
4 rows in set (0.00 sec)

关于mysql - 两列之间的关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58212676/

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