Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a new table from merging two tables with union

Tags:

mysql

I have two tables with the same columns.

I can merge them with UNION

select * from  table1
union
select * from table2;

How do I create a new table with the same contents and columns as that query?

like image 709
Jonathan Raul Tapia Lopez Avatar asked Feb 15 '12 11:02

Jonathan Raul Tapia Lopez


2 Answers

You can use CREATE TABLE ... SELECT statement.

CREATE TABLE new_table
  SELECT * FROM table1
    UNION
  SELECT * FROM table2;
like image 186
Devart Avatar answered Dec 28 '22 22:12

Devart


create table new_table as
select col1, col2 from table1 
union 
select col1, col2 from table2

Make sure you select same set of columns from tables in union.

like image 22
Husain Basrawala Avatar answered Dec 29 '22 00:12

Husain Basrawala