I have the following Tables:
Table A
id | name
1 | foo
2 | bar
Table B
id | name | default
1 | 123 | 1
2 | 321 | 0
3 | 456 | 1
Table C
A_id | B_id
1 | 2
1 | 3
now I would like to get the A-B pairs (with condition that these pairs have B.default = 1) which are not in C
So the expected result would be:
A_id | B_id
1 | 1
2 | 1
2 | 3
but I’m totally confused about how to join table B in:
SELECT A.id, B.id
FROM A
LEFT OUTER JOIN C
ON C.A_id = A.id
LEFT OUTER JOIN B
AND C.B_id = B.id
AND B.default = 1
Generate all A-pairs first, then compare to C
The NOT EXISTS is slightly more correct (anti-semi-join) than a LEFT JOIN
However, this gives this result. Only the C tuple of (1,3) matches the 4 A-B pairs