Say we have the following tables t1 and t2:
t1:
id | column_1
-------------
1 | 1
2 | 2
t2:
id | column_2
-------------
2 | 2
3 | 3
and we want to find the following result:
id | column_1 | column_2
------------------------
1 | 1 |
2 | 2 | 2
3 | | 3
This basically is the union of a right join with a left join. The following code works but feels clumsy:
(
SELECT t1.id, t1.column_1, t2.column_2
FROM t1
LEFT JOIN t2 ON t1.id = t2.id
)
UNION
(
SELECT t2.id, t1.column_1, t2.column_2
FROM t1
RIGHT JOIN t2 ON t1.id = t2.id
)
Is there a better way to achieve this?
t1
(id
int(11) NOT NULL,column_1
int(11) NOT NULL ); INSERT INTOt1
(id
,column_1
) VALUES (1, 1), (2, 2); CREATE TABLE IF NOT EXISTSt2
(id
int(11) NOT NULL,column_2
int(11) NOT NULL ); INSERT INTOt2
(id
,column_2
) VALUES (2, 2), (3, 3);