繁体   English   中英

Select 多行,其中一列相同

[英]Select multiple row where one of their columns is the same

我想要 select 一行,其中另一行具有相同用户 ID 是活动的。 喜欢:

我的表

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

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

您可以使用带有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")

Output

现场状态
汤姆

您可以使用exists检查相同的用户 ID 值

select *
from mytable t
where exists (
  select * from mytable t2
  where t2.userid=t.userid
    and t2.userfield='account' 
    and t2.fieldstatus='Active'

)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM