为此,请在MySQL中使用INSERT INTO SELECT语句。让我们创建一个表-
mysql> create table demo61 −> ( −> id int, −> name varchar(20) −> ) −> ;
借助insert命令将一些记录插入表中-
mysql> insert into demo61 values(1,'John'); mysql> insert into demo61 values(2,'David'); mysql> insert into demo61 values(1,'Mike'); mysql> insert into demo61 values(2,'Carol'); mysql> insert into demo61 values(2,'Bob');
使用select语句显示表中的记录-
mysql> select *from demo61;
这将产生以下输出-
+------+-------+ | id | name | +------+-------+ | 1 | John | | 2 | David | | 1 | Mike | | 2 | Carol | | 2 | Bob | +------+-------+ 5 rows in set (0.00 sec)
以下是创建第二个表的查询。
mysql> create table demo62 -> ( −> employee_id int not null auto_increment primary key, −> employee_name varchar(20) −> );
借助insert命令将一些记录插入表中-
mysql> insert into demo62(employee_name) −> select name from demo61 where id=2; Records: 3 Duplicates: 0 Warnings: 0
使用select语句显示表中的记录-
mysql> select *from demo62;
这将产生以下输出-
+-------------+---------------+ | employee_id | employee_name | +-------------+---------------+ | 1 | David | | 2 | Carol | | 3 | Bob | +-------------+---------------+ 3 rows in set (0.00 sec)