简体   繁体   中英

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_id s 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 ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM