SQL left outer join on multiple columns

According to this SQL join cheat-sheet , the left outer join in one column is as follows:

SELECT *
  FROM a
  LEFT JOIN b 
    ON a.foo = b.foo
  WHERE b.foo IS NULL 

I am wondering what it would look like with a join in multiple columns, should it be in the sentence OReither ?ANDWHERE

SELECT *
  FROM a
  LEFT JOIN b 
    ON  a.foo = b.foo
    AND a.bar = b.bar
    AND a.ter = b.ter
WHERE b.foo IS NULL 
  OR  b.bar IS NULL 
  OR  b.ter IS NULL

or

SELECT *
  FROM a
  LEFT JOIN b 
    ON  a.foo = b.foo
    AND a.bar = b.bar
    AND a.ter = b.ter
WHERE b.foo IS NULL 
  AND b.bar IS NULL 
  AND b.ter IS NULL

?

(I don't think so, but if that matters, the db engine is Vertica)

(I bet on OR)

+4
source share
2 answers

It depends on whether the columns are null, but if they are not, then checking for any of them will be:

SELECT *
  FROM a
  LEFT JOIN b 
    ON  a.foo = b.foo
    AND a.bar = b.bar
    AND a.ter = b.ter
WHERE b.foo IS NULL -- this could also be bar or ter

, .

NULL, , - , (OR) .

+8

:

SELECT *
FROM a
LEFT JOIN b ON a.foo = b.foo AND a.bar = b.bar AND a.ter = b.ter

WHERE . WHERE b.foo IS NULL a, b b.foo null.

+1

Source: https://habr.com/ru/post/1657586/


All Articles