I would like to merge 2 tables keeping all rows in both tables like doing a left and a right join at the same time.
See example below. Column ‘fruit’ is common to both tables and I want to list the number of fruit in both tables. Also a particular fruit may appear in one table but not the other.
Can anyone help? Thanks.
TABLE1 TABLE2
fruit, number fruit, number
------------- -------------
apples, 1 apples, 10
pears, 2 oranges, 30
MERGED TABLE (this is the result I'm after:
fruit, number_table1, number_table2
--------------------------------------
apples, 1, 10
pears, 2, -
oranges, -, 30
And here is the code to create the tables if you need to try it out….
CREATE TABLE table1 (fruit CHAR(10) NOT NULL, number INT(10) NOT NULL);
CREATE TABLE table2 (fruit CHAR(10) NOT NULL, number INT(10) NOT NULL);
insert into table1 (fruit, number) values ('apples', 1), ('pears', 2);
insert into table2 (fruit, number) values ('apples', 10), ('oranges', 30);
Here is a solution using UNION: