我有此数据。
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?
您可以将correlated subquery
与not 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'))