怎樣從一列複製到另一列(MySQL 中的不同表同時資料庫)?


要從一列複製到另一列,可以使用 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)

更新於: 21-Aug-2019

超過 5K 的瀏覽量

開啟你的 職業生涯

完成課程,獲得認證

開始
廣告
© . All rights reserved.