gpt4 book ai didi

mysql - 检测表格中的模式

转载 作者:可可西里 更新时间:2023-11-01 06:33:35 25 4
gpt4 key购买 nike

有一个这样的表:

Table: statuses
ID | status
----|---------
777 | 1
675 | 1
651 | 0
611 | 1
600 | 0
554 | 1
443 | 0
323 | 0
222 | 1
112 | 1

如何只选择行中两个(或多个)状态为0的行?(示例中只有 443、323),并按系列中的第一个 ID 将它们分组。

所以输出将是:

ID  | status | group
----|--------|---------
443 | 0 | 443
323 | 0 | 443

最佳答案

如果编程方法不可行,您可以尝试此查询(尽管它的性能会更差)。

 select
s.id,
0 as status,
group_field.id as group
from
statuses s,
(select id from statuses where status = 0 group by id having count(1) > 1 limit 1) group_field
where
s.id in(select id from statuses where id = 0 group by id having count(1) > 1)

关于mysql - 检测表格中的模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29669586/

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