首页 > 解决方案 > 选择其中一列相同的多行

问题描述

我想选择另一行中具有相同用户 ID 的行是 Active 。喜欢 :

我的表

用户身份 用户域 现场状态
1 姓名 汤姆
1 帐户 积极的
2 姓名 杰瑞
2 帐户 失败的

我想在其帐户处于活动状态的名称用户字段中选择 tom。我在用python写

标签: sqlpython-3.x

解决方案


您可以使用子查询exists

select t.fieldstatus from mytable t where t.userfield = "name" and exists (select 1 from mytable t1 where t1.userid = t.userid and t1.userfield = "account" and t1.fieldstatus = "Active")

输出

现场状态
汤姆

推荐阅读