MYSQL: join two tables into one, with a join

I need to make a table from two other tables (and use union). Request in progress:

SELECT * FROM tabel1 UNION SELECT * FROM tabel2 

Now I need to do this result (data) in table3 (the table I already have the same columns as in table1 and table2).

Who can help me?

+4
source share
1 answer
 INSERT INTO table3 SELECT * FROM tabel1 UNION SELECT * FROM tabel2 

since you have the same columns in all three of them ...

In general, you should work with column lists, e.g.

 INSERT INTO table3 (col1, col2, col3) SELECT col1, col2, col3 FROM tabel1 UNION SELECT col1, col2, col3 FROM tabel2 

This way you avoid problems with auto_increment identity columns. You should also consider using UNION ALL , since UNION filters out duplicate rows and therefore takes longer on large tables.

+16
source

Source: https://habr.com/ru/post/1500346/


All Articles