0

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

user2251695
  • 129
  • 10

1 Answers1

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.

G Wimpassinger
  • 751
  • 5
  • 18
  • Does this create a new table? – Hashim Aziz Apr 20 '23 at 03:49
  • 1
    @HashimAziz: No, this is just the "concatenation" of two select statements. What makes you think, this coudl create a new table? But you can create a new table and fill it directly with something like ```CREATE TABLE NewTable AS SELECT * FROM OldTable```. See the mysql [docs](https://dev.mysql.com/doc/refman/8.0/en/create-table-select.html) – G Wimpassinger Apr 20 '23 at 08:58