Tuesday 4 September 2018

The outer outer join does not work on the column with id repeated

This is table1:

This is table2:
I wanted to see this result:
I wrote this query:
select title, value
from table1
left outer join table2
    on table1.id = table2.id
where category="good"

But it gives me this result:
So, what query should I use to get result with title c coming with ""? (empty string)

Move the predicate on table2.category to the ON clause, rather than the WHERE clause.
(In the WHERE clause, that negates the "outerness" of the LEFT JOIN operation, since any rows from table1 with no matching row from table2 would have values of NULL for the table2 columns. Checking for a non-null value excludes all the "unmatched" rows, rendering the LEFT JOIN equivalent to an INNER JOIN.
One way to return the specified resultset:
SELECT t.title
     , s.value
  FROM table1 t
  LEFT
  JOIN table2 s
    ON s.id = t.id
   AND s.category = "good"

0 comments:

Post a Comment