将 WHERE 子句应用于一行中的公共元素

Apply WHERE clause to common elements in a row

我有一个 table 这样的:

     ID  pid  et  time  action 
 1   A    a    1    t1    -
 2   A    b    5    t2    r
 3   B    c    1    t3    -
 4   B    d    3    t4    -
 5   B    e    5    t5    r
 6   C    f    1    t6    -
 7   C    g    1    t7    -

我想在 where 子句中对满足特定 ID 下特定条件的 return 行应用过滤器。

我想return符合这些条件的所有 ID

结果应该是:

   ID pid et time action
1  A   a   1  t1    - 
2  A   b   5  t2    r
3  B   c   1  t3    -
4  B   d   3  t4    -
5  B   e   5  t5    r

因为ID=C是上面table中唯一没有满足上面两个条件的C的ID

如果这些都是 where 子句应该满足的条件 ...

where (action = 'r') OR (et= 3)

但这将 return 只有第 2、4、5 行。

为了您想要的结果,您应该使用这个:

select * from t where ID in (select ID from t where (action = 'r') OR (et=3))

您可以使用 exists 检查每个 Id 匹配项

select * 
from t
where exists (
    select * from t t2 
    where t2.id=t.id and (t2.et=3 or t2.action='r')
)

See DB<>Fiddle