MySQL INSERT INTO SELECT 导致一次从另一个表插入多行
mysqlmysqli database更新于 2025/11/5 0:12:17
首先我们创建一个表 −
mysql> create table DemoTable(Name varchar(100)); Query OK, 0 rows affected (0.55 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable1 values('Chris');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable1 values('Robert');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable1 values('Adam');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable1 values('Bob');
Query OK, 1 row affected (0.20 sec)
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable1;
这将产生以下输出 −
+--------+ | Name | +--------+ | Chris | | Robert | | Adam | | Bob | +--------+ 4 rows in set (0.00 sec)
以下是创建第二个表的查询。
mysql> create table DemoTable2(ListOfEmployee text); Query OK, 0 rows affected (0.59 sec)
以下是执行 INSERT INTO SELECT 的查询,结果插入了多行 −
mysql> insert into DemoTable2(ListOfEmployee) select group_concat(Name) from DemoTable1; Query OK, 1 row affected (0.20 sec) Records − 1 Duplicates − 0 Warnings − 0
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable2;
这将产生以下输出 −
+-----------------------+ | ListOfEmployee | +-----------------------+ | Chris,Robert,Adam,Bob | +-----------------------+ 1 row in set (0.00 sec)
相关文章
有用资源
mysql 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/

