在MySQL中将数据从一个表插入到另一个表?
要将数据从一个表插入到另一个表,请使用INSERTINTOSELECT语句。让我们首先创建一个表-
mysql> create table DemoTable1 -> ( -> Id int, -> FirstName varchar(20) -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1 values(101,'Adam'); mysql> insert into DemoTable1 values(102,'John');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable1;
这将产生以下输出-
+------+-----------+ | Id | FirstName | +------+-----------+ | 101 | Adam | | 102 | John | +------+-----------+ 2 rows in set (0.00 sec)
以下是创建第二个表的查询-
mysql> create table DemoTable2 -> ( -> EmployeeId int, -> EmployeeName varchar(20) -> );
这是在MySQL中将数据从一个表插入到另一个表的查询-
mysql> insert into DemoTable2(EmployeeId,EmployeeName) select Id,FirstName from DemoTable1; Records: 2 Duplicates: 0 Warnings: 0
使用select语句显示表中的所有记录-
mysql> select *from DemoTable2;
这将产生以下输出-
+------------+--------------+ | EmployeeId | EmployeeName | +------------+--------------+ | 101 | Adam | | 102 | John | +------------+--------------+ 2 rows in set (0.00 sec)
热门推荐
10 毛笔哥哥结婚祝福语简短
11 向国庆送祝福语简短
12 建队节祝福语简短
13 朋友喜得外孙简短祝福语
14 小店营业的祝福语简短
15 餐饮生日祝福语简短独特
16 鲜花英语祝福语卡片简短
17 男朋友暴富祝福语简短
18 婶婶拜年祝福语大全简短