Skip to content
Advertisement

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

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 ?

Advertisement

Answer

select id from table 
where
value  in ('A', 'B')
group by id 
having count( distinct value ) = 2
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement