You can use group by
and having
:
select set_id
from many_relation
where element_id in (1, 2)
group by set_id
having count(*) = 2; -- the "2" is the number of items in the `in` list
EDIT:
In Postgres, you can do this using arrays. For instance:
select set_id
from many_relation
where element_id = any (array[1, 2])
group by set_id
having count(*) = cardinality(array[1, 2]);
This repeat the array for clarity. You can define it using a CTE (for instance). However, I suspect you are passing it in as a parameter, so ?
would just be in both places.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…