I have two tables: pq and pe. I am trying to LEFT OUTER JOIN
left table (pq) on right table (pe).
WHERE pe.uid = "12345"
)Here is how tables look:
pq:
id | data
1 | "abc"
2 | "efg"
pe:
pqid | uid | data
2 | 54321 | "uvw"
2 | 12345 | "xyz"
I can use the following query to match first 2 rows of pq.id to pe.pqid
SELECT pq.id, pq.data, pe.data FROM pq
LEFT OUTER JOIN pe ON pq.id = pe.pqid
ORDER BY pq.id LIMIT 2
I get:
pq.id | pq.data | pe.data
1 | "abc" |
2 | "efg" | "uvw"
But if I use the WHERE statement like this:
SELECT pq.id, pq.data, pe.data FROM pq
LEFT OUTER JOIN pe ON pq.id = pe.pqid
WHERE pe.uid='12345'
ORDER BY pq.id LIMIT 2
I only get one row with matching pe.pqid AND pe.uid:
pq.id | pq.data | pe.data
2 | "efg" | "xyz"
So with the WHERE clause I get the right pe.data, but I don't get pq rows that have no pq.id matching pe.pqid
I need to get this:
pq.id | pq.data | pe.data
1 | "abc" |
2 | "efg" | "xyz"
In SQL Full Outer Join, all rows from both the tables are included. If there are any unmatched rows, it shows NULL values for them.
When performing an inner join, rows from either table that are unmatched in the other table are not returned. In an outer join, unmatched rows in one or both tables can be returned.
You can use LEFT OUTER JOIN to return unmatched tuples from the table mentioned on its left, RIGHT for the table mentioned on its RIGHT, or FULL to get both. Note that the word “OUTER” is optional and assumed in most DBMS if you specify LEFT, RIGHT, or FULL.
Yes. The where
clause is turning the left outer join into an inner join.
Why? The value of pe.pqid
is NULL
(as is pe.uid
) when there is no match. So the comparison in the where
clause fails (almost all comparisons to NULL
return NULL
which is considered false).
The solution is to move the comparison to the on
clause:
SELECT pq.id, pq.data, pe.data
FROM pq LEFT OUTER JOIN
pe
ON pq.id = pe.pqid and
pe.uid='12345'
ORDER BY pq.id LIMIT 2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With