views:

60

answers:

2

According to Google search: since MySQL does not support full outer join, it could be simulated via union and/or union all. But both of these either remove genuine duplicates or show spurious duplicates.

What would be correct and efficient way?

This question seems relevant but couldn't get the answer of it.

A: 

As mentioned by @haim evgi, union all and exlusion join do the trick (2nd method mentioned on the page). Since he mentioned the link in comment, I can't accept his answer.

understack
+2  A: 

You can use a LEFT JOIN and a RIGHT JOIN:

SELECT * FROM tableA LEFT JOIN tableB ON tableA.b_id = tableB.id
UNION ALL
SELECT * FROM tableA RIGHT JOIN tableB ON tableA.b_id = tableB.id
WHERE tableA.b_id IS NULL

There is also some information on Wikipedia about this topic: Full outer join.

The Wikipedia article suggests using a UNION in MySQL. This is slightly slower than UNION ALL, but more importantly it won't always give the correct result - it will remove duplicated rows from the output. So prefer to use UNION ALL instead of UNION here.

Mark Byers
This is exactly the solution mentioned by @haim evgi (via page link). Thanks.
understack