在 MySQL 中从一列复制到另一列(不同表同一数据库)?

mysqlmysqli database更新于 2025/10/3 11:24:17

要从一列复制到另一列,可以使用 INSERT INTO SELECT 语句。

首先我们创建一个表 −

mysql> create table DemoTable1 (PlayerScore int);
Query OK, 0 rows affected (0.46 sec)

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

mysql> insert into DemoTable1 values(98);
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable1 values(81);
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable1 values(76);
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable1 values(88);
Query OK, 1 row affected (0.15 sec)

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

mysql> select *from DemoTable1;

这将产生以下输出 -

+-------------+
| PlayerScore |
+-------------+
| 98          |
| 81          |
| 76          |
| 88          |
+-------------+
4 rows in set (0.00 sec)

这是创建第二个表的查询 −

mysql> create table DemoTable2 (Marks int);
Query OK, 0 rows affected (0.47 sec)

这是从一列复制到另一列的查询(不同的表相同的数据库)MySQL −

mysql> insert into DemoTable2(Marks) select PlayerScore from DemoTable1;
Query OK, 4 rows affected (0.19 sec)
Records: 4 Duplicates: 0 Warnings: 0

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

mysql> select *from DemoTable2;

这将产生以下输出 -

+-------+
| Marks |
+-------+
| 98    |
| 81    |
| 76    |
| 88    |
+-------+
4 rows in set (0.00 sec)

相关文章


有用资源