Warm tip: This article is reproduced from stackoverflow.com, please click
postgresql sql relational-division

How to use WHERE query at same column in same table?

发布于 2020-03-28 23:15:43

I have this data.

tbl_data

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

I want to select id which have 'A' and 'B' as value.

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

But it returns zero result.

How to make it to return id 1 ?

Questioner
Yusufmm
Viewed
27
Oto Shavadze 2020-01-31 17:52
select id from table 
where
value  in ('A', 'B')
group by id 
having count( distinct value ) = 2