MySQL 通过合并两个表创建一个新表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9292313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 12:08:20  来源:igfitidea点击:

Create a new table from merging two tables with union

mysql

提问by Jonathan Raul Tapia Lopez

I have two tables with the same columns.

我有两个具有相同列的表。

I can merge them with UNION

我可以将它们合并 UNION

select * from  table1
union
select * from table2;

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

如何创建与该查询具有相同内容和列的新表?

回答by Devart

You can use CREATE TABLE ... SELECTstatement.

您可以使用CREATE TABLE ... SELECT语句。

CREATE TABLE new_table
  SELECT * FROM table1
    UNION
  SELECT * FROM table2;

回答by Husain Basrawala

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.

确保从联合表中选择相同的一组列。

回答by kkjava

Or even you can explicitly define create table (we generally use in our project).

或者甚至你可以显式定义创建表(我们通常在我们的项目中使用)。

CREATE TABLE IF NOT EXISTS auditlog (

如果不存在,则创建表审计日志 (

user_id varchar(30) NOT NULL default '',

user_id varchar(30) NOT NULL 默认值 '',

user_ip varchar(255) NOT NULL default '',

user_ip varchar(255) NOT NULL 默认值 '',

........ ........

………………

KEY ie1 (user_id) )

KEY ie1 (user_id) )

union=(auditlog_2,auditlog_3,auditlog_4) engine=merge insert_method=last;

union=(auditlog_2,auditlog_3,auditlog_4) engine=merge insert_method=last;