我們可以藉助 MySQL 自計算輸出將值插入表中嗎?


我們可以藉助 MySQL 返回的自計算輸出將值插入表中。在這種情況下,我們不需要使用虛擬的“dual”表。語法可以如下所示 −

INSERT INTO table_name(column1,column2,column3,…) Select value1,value2,value3,…;

示例

在以下示例中,我們使用 MySQL 自計算輸出在“testing”表中插入了值。

mysql> Create table testing(id int, item_name varchar(10));
Query OK, 0 rows affected (0.15 sec)

mysql> Insert into testing (id,item_name)Select 1,'Book';
Query OK, 1 row affected (0.11 sec)
Records: 1 Duplicates: 0 Warnings: 0

mysql> Insert into testing (id,item_name)Select 2,'Pen';
Query OK, 1 row affected (0.11 sec)
Records: 1 Duplicates: 0 Warnings: 0

mysql> select * from testing;

+------+-----------+
| id   | item_name |
+------+-----------+
| 1    | Book      |
| 2    | Pen       |
+------+-----------+

2 rows in set (0.00 sec)

更新於:20-Jun-2020

97 次瀏覽

開啟您的職業生涯

透過完成課程獲得認證

入門
廣告
© . All rights reserved.