I wish to join two (or more) tables based on a set of columns that are present in all tables that take part in the join. In other words I wish to join tables based on the intersection set of columns. However each table has extra columns which are unique to that table.
Example
#: A number
-: NULL
Table A
+------+------+------+
| Col1 | Col2 | ColA |
+------+------+------+
| A | A | # |
| A | B | # |
+------+------+------+
Table B
+------+------+------+
| Col1 | Col2 | ColB |
+------+------+------+
| A | A | # |
| B | B | # |
+------+------+------+
Result
+------+------+------+------+
| Col1 | Col2 | ColA | ColB |
+------+------+------+------+
| A | A | # | # |
| A | B | # | - |
| B | B | - | # |
+------+------+------+------+
I've come up with a solution but the performance is horrid, performance is an issue. I don't want to pollute you with that solution. I'd much rather have a fresh set of eyes on this :)
Looking forward to seeing your solutions. Thank you for your time. It's much appreciated.
UPDATE
Thank you for all the responds. However it seems I didn't explain the problem well enough. (Haven't tested all answers yet)
But note how Table B has a row that's not present in Table A.
Table B
+------+------+------+
| Col1 | Col2 | ColB |
+------+------+------+
| B | B | # |
+------+------+------+
And Table A vice versa.
The solution I've come up with unions all tables together on the intersection set of columns to generate a skeleton.
Skeleton:
SELECT Col1, Col2 FROM TableA
UNION
SELECT Col1, Col2 FROM TableB
Once I have the skeleton I LEFT OUTER JOIN for each table.
LEFT OUTER JOIN TableA AS a ON a.Col1=skeleton.Col1 AND a.Col2=skeleton.Col2
LEFT OUTER JOIN TableB AS b ON b.Col1=skeleton.Col1 AND b.Col2=skeleton.Col2
So the final query looks like this
SELECT s.*, a.ColA, b.ColB
FROM
(
SELECT Col1, Col2
FROM TableA
UNION
SELECT Col1, Col2
FROM TableB
) s
LEFT OUTER JOIN TableA a ON a.Col1=s.Col1 AND a.Col2=s.Col2
LEFT OUTER JOIN TableB b ON b.Col1=s.Col1 AND b.Col2=s.Col2