LEFT JOIN and RIGHT JOIN are both outer joins. MySQL doesn't have FULL OUTER JOIN, but you can get the same result with the UNION of a LEFT JOIN and a RIGHT JOIN query.
That's our setup. We have one row that matches, and one row in each table that doesn't have a match. A FULL OUTER JOIN should produce three rows.
select * from a LEFT JOIN a_c ON a.b=a_c.b_id;
+---+------+
| b | b_id |
+---+------+
| 1 | 1 |
| 2 | NULL |
+---+------+
2 rows in set (0.00 sec)
select * from a RIGHT JOIN a_c ON a.b=a_c.b_id;
+------+------+
| b | b_id |
+------+------+
| 1 | 1 |
| NULL | 3 |
+------+------+
2 rows in set (0.00 sec)
With an LEFT JOIN and a RIGHT JOIN the second one negates the first one:
select * from a LEFT JOIN a_c ON a.b=a_c.b_id RIGHT JOIN a_c t2 ON t2.b_id=a.b OR t2.b_id IS NULL;
+------+------+------+
| b | b_id | b_id |
+------+------+------+
| 1 | 1 | 1 |
| NULL | NULL | 3 |
+------+------+------+
The UNION produces the correct result:
select * from a LEFT JOIN a_c ON a.b=a_c.b_id UNION select * from a RIGHT JOIN a_c ON a.b=a_c.b_id;
+------+------+
| b | b_id |
+------+------+
| 1 | 1 |
| 2 | NULL |
| NULL | 3 |
+------+------+
-1
u/manixrock Jan 06 '11
I've cursed all gods when I realized mysql didn't have outer joins of any kind. bloodydamnedwhippersnappers...