首页 文章资讯内容详情

将表的所有行复制到MySQL中的另一个表?

2026-06-05 1 花语

要将表的所有行复制到另一个表,请使用以下语法-

insert into yourTableName2(yourColumnName1,...N) select yourColumnName1,..N from yourTableName1;

让我们首先创建一个表-

create table DemoTable1(FirstName varchar(100));

使用插入命令在表中插入一些记录-

insert into DemoTable1 values(John); insert into DemoTable1 values(Chris); insert into DemoTable1 values(Bob);

使用select语句显示表中的所有记录-

select *from DemoTable1;

这将产生以下输出-

+-----------+ | FirstName | +-----------+ | John | | Chris | | Bob | +-----------+ 3 rows in set (0.00 sec)

以下是创建第二个表的查询-

create table DemoTable2 (EmployeeName varchar(100));

以下是将表的所有行复制到另一个表的查询-

insert into DemoTable2(EmployeeName) select FirstName from DemoTable1; Records: 3 Duplicates: 0 Warnings: 0

现在让我们来检查第二个表,其中我们设置1代的记录的记录ST表-

select *from DemoTable2;

这将产生以下输出-

+--------------+ | EmployeeName | +--------------+ | John | | Chris | | Bob | +--------------+ 3 rows in set (0.00 sec)