Ilja
Ilja

Reputation: 1215

how to select rows for combination of column values

I have table search_settings with content as following:

    +-----------+-------------+----------+
    | entity_id | language_id | query_id |
    +-----------+-------------+----------+
    |         2 |          71 |      141 |
    |         3 |          72 |      141 |
    |         4 |          73 |      141 |
    |         4 |          74 |      142 |
    |         4 |          75 |      142 |
    +-----------+-------------+----------+

How to select unique query_ids for which, there are existing entries with language_id=71 and language_id=72 and language_id=73 ?

For example i need query_id = 142 row if language_id=74 and language_id=75, but if language_id=999 and language_id=75 it should return nothing ? is it possible ?

Upvotes: 0

Views: 52

Answers (1)

Vamsi Prabhala
Vamsi Prabhala

Reputation: 49260

Use group by with having to check if a query_id has all the required language_id's associated with it.

select query_id
from tablename
group by query_id
having sum(case when language_id in (74,75) then 1 else -1 end) = 2

Upvotes: 1

Related Questions