如何在同一表的同一列上使用WHERE查询?

问题描述 投票:0回答:1

我有此数据。

tbl_data

id    value
1       A
1       B
1       C
2       A
2       C

我想选择具有'A'和'B'作为值的ID。

SELECT id FROM tbl_data WHERE value = 'A' AND value = 'B'

但是它返回零结果。

如何使其返回ID 1?

sql postgresql relational-division
1个回答
0
投票

您可以将correlated subquerynot exists一起使用-

select distinct id from tablename a
where not exists (select 1 from tablename b where a.id=b.id and value not in ('A','B')) 
© www.soinside.com 2019 - 2024. All rights reserved.