從一張表將幾列複製到 MySQL 中的另一張表中


讓我們首先建立一個表 -

mysql> create table DemoTable1
   -> (
   -> Id int,
  -> Name varchar(100)
   -> );
Query OK, 0 rows affected (0.53 sec)

使用 insert 命令在表中插入一些記錄 -

mysql> insert into DemoTable1 values(10,'John');
Query OK, 1 row affected (0.23 sec)

mysql> insert into DemoTable1 values(11,'Chris');
Query OK, 1 row affected (0.14 sec)

mysql> insert into DemoTable1 values(12,'Robert');
Query OK, 1 row affected (0.32 sec)

使用 select 語句顯示錶中的所有記錄 -

mysql> select *from DemoTable;

輸出

這將產生以下輸出 -

+------+--------+
| Id   | Name   |
+------+--------+
|   10 | John   |
|   11 | Chris  |
|   12 | Robert |
+------+--------+
3 rows in set (0.00 sec)

以下是從 DemoTable1 表中複製一些列到另一張表 DemoTable2 中的查詢 -

mysql> create table DemoTable2 (
-> select Name from DemoTable1 where Id=11
-> );
Query OK, 1 row affected (0.70 sec)
Records: 1 Duplicates: 0 Warnings: 0

現在檢查表 'DemoTable2' 的表記錄 -

mysql> select *from DemoTable2;

輸出

這將產生以下輸出 -

+-------+
| Name  |
+-------+
| Chris |
+-------+
1 row in set (0.00 sec)

更新於: 30-Jun-2020

308 次瀏覽

開啟你的 職業

透過完成課程來獲得認證

開始
廣告