'joining two tables with different columns into one
I have 2 tables that I want to join. They both have a uuid field and a text name field. Besides that they have no connection, The end result I wish to achieve is one table that combined all names and all uuid fields.
SELECT tbl1.uuid as uuid,
tbl1.nname as tname,
tbl2.uuid as uuid,
tble2.zname as tname
FROM some_table_1 tbl1
CROSS JOIN some_table_2 tbl2
the query above creates 2 uuid columns and 2 name columns
Solution 1:[1]
You can use the UNION
operator. With UNION ALL
every row from both tables are in the result set. If you use UNION
only distinct rows will be in the result set.
SELECT uuid, nname as tname FROM some_table_1
UNION ALL
SELECT uuid, zname as tname FROM some_table_2
Have a look at this answer about a good explanation of UNION
.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |