How can we insert values into a table with the help of MySQL self-computed output?

MySQLMySQLi Database

We can insert the values into a table with the help of the self-computed output returned by MySQL. In this case, we do not need to use dummy ‘dual’ table. The syntax can be as follows −

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

Example

In the example below, we have inserted the values in ‘testing’ table by using the MySQL self-computed output.

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)
raja
Published on 19-Feb-2018 10:15:42
Advertisements