If I interpret your question correctly:
"Pick all rows where a
has one of two given values, and b
only exists in combination with one of them."
You can use any of the standard techniques laid out in the referenced answer.
- Select rows where value of second column is not present in first columnSelect rows where value of second column is not present in first column
Just restrict your base table to the two given a
. For instance
SELECT *
FROM tbl t1
WHERE a IN (1,2)
AND NOT EXISTS (
SELECT 1
FROM tbl t2
WHERE a IN (1,2)
AND t2.b = t1.b
AND t2.a <> t1.a
);
This also works with duplicates on (a,b)
. Multiple identical rows would be returned.