在 MySQL 中使用 Select 查询插入

mysqlmysqli database更新于 2024/8/11 10:52:17

对于使用 SELECT 查询插入,语法如下 −

insert into yourTableName(yourColumnName1,yourColumnName2,yourColumnName3,...N) select yourValue1,yourValue2,yourValue3,......N;

首先我们创建一个表 −

mysql> create table DemoTable1603
   -> (
   -> StudentId int,
   -> StudentName varchar(20),
   -> StudentMarks int
   -> );
Query OK, 0 rows affected (0.50 sec)

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

mysql> insert into DemoTable1603(StudentId,StudentName,StudentMarks) select 101,'John',45;
Query OK, 1 row affected (0.17 sec)
Records: 1  Duplicates: 0  Warnings: 0
mysql> insert into DemoTable1603(StudentId,StudentName,StudentMarks) select 102,'Adam',76;
Query OK, 1 row affected (0.11 sec)
Records: 1  Duplicates: 0  Warnings: 0
mysql> insert into DemoTable1603(StudentId,StudentName,StudentMarks) select 103,'Bob',67;
Query OK, 1 row affected (0.31 sec)
Records: 1  Duplicates: 0  Warnings: 0

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

mysql> select * from DemoTable1603;

这将产生以下输出 −

+-----------+-------------+--------------+
| StudentId | StudentName | StudentMarks |
+-----------+-------------+--------------+
|       101 | John        |           45 |
|       102 | Adam        |           76 |
|       103 | Bob         |           67 |
+-----------+-------------+--------------+
3 rows in set (0.00 sec)

相关文章


有用资源