我们可以通过MySQL返回的自计算输出将值插入到表中。在这种情况下,我们不需要使用虚拟的“dual”表。语法可以如下所示:
INSERT INTO table_name(column1,column2,column3,…) Select value1,value2,value3,…;
Example
在下面的示例中,我们使用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)