MySQL INSERT INTO SELECT导致从另一张表一次插入多行

让我们首先创建一个表-

mysql> create table DemoTable(Name varchar(100));

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

mysql> insert into DemoTable1 values('Chris');
mysql> insert into DemoTable1 values('Robert');
mysql> insert into DemoTable1 values('Adam');
mysql> insert into DemoTable1 values('Bob');

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

mysql> select *from DemoTable1;

这将产生以下输出-

+--------+
| Name   |
+--------+
| Chris  |
| Robert |
| Adam   |
| Bob    |
+--------+
4 rows in set (0.00 sec)

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

mysql> create table DemoTable2(ListOfEmployee text);

以下是执行INSERT INTO SELECT的查询,导致插入了多行-

mysql> insert into DemoTable2(ListOfEmployee) select group_concat(Name) from DemoTable1;
Records − 1 Duplicates − 0 Warnings − 0

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

mysql> select *from DemoTable2;

这将产生以下输出-

+-----------------------+
| ListOfEmployee        |
+-----------------------+
| Chris,Robert,Adam,Bob |
+-----------------------+
1 row in set (0.00 sec)