为此,请使用INSERT INTO SELECT语句。让我们首先创建一个表-
mysql> create table DemoTable1879 ( Id int, Name varchar(20) );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1879 values(101,'Chris Brown'); mysql> insert into DemoTable1879 values(102,'David Miller'); mysql> insert into DemoTable1879 values(103,'Adam Smith');
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1879;
这将产生以下输出-
+------+--------------+ | Id | Name | +------+--------------+ | 101 | Chris Brown | | 102 | David Miller | | 103 | Adam Smith | +------+--------------+ 3 rows in set (0.00 sec)
这是创建第二个表的查询-
mysql> create table DemoTable1880 ( ClientId int, ClientName varchar(20) );
这是将行从一个表复制到另一个表的查询-
mysql> insert into DemoTable1880(ClientId,ClientName) select Id,Name from DemoTable1879 where Id IN(101,103); Records: 2 Duplicates: 0 Warnings: 0
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1880;
这将产生以下输出-
+----------+-------------+ | ClientId | ClientName | +----------+-------------+ | 101 | Chris Brown | | 103 | Adam Smith | +----------+-------------+ 2 rows in set (0.00 sec)