将MySQL表的所有行复制到另一个表?
要将一个表的所有行复制到另一个表中,请使用以下语法-
insert into yourTableName2(yourColumnName1,...N) select yourColumnName1,..N from yourTableName1;
让我们首先创建一个表 –
mysql> create table DemoTable1(FirstName varchar(100));
Query OK, 0 rows affected (1.11 sec)
使用insert命令向该表插入一些记录 –
mysql> insert into DemoTable1 values('John');
Query OK, 1 row affected (0.31 sec)
mysql> insert into DemoTable1 values('Chris');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable1 values('Bob');
Query OK, 1 row affected (0.40 sec)
使用select语句显示表中的所有记录 –
mysql> select *from DemoTable1;
这将产生以下输出 –
+-----------+
| FirstName |
+-----------+
| John |
| Chris |
| Bob |
+-----------+
3 rows in set (0.00 sec)
下面是创建第二个表的查询 –
mysql> create table DemoTable2 (EmployeeName varchar(100));
Query OK, 0 rows affected (0.78 sec)
以下是复制一个表的所有行到另一个表的查询-
mysql> insert into DemoTable2(EmployeeName) select FirstName from DemoTable1;
Query OK, 3 rows affected (0.52 sec)
Records: 3 Duplicates: 0 Warnings: 0
现在让我们检查第二个表的记录,其中我们设置了第一个表的记录 –
mysql> select *from DemoTable2;
这将产生以下输出 –
+--------------+
| EmployeeName |
+--------------+
| John |
| Chris |
| Bob |
+--------------+
3 rows in set (0.00 sec)
阅读更多:MySQL 教程