How to multiple insert or batch insert at a time in MySQL query?

MySQLMySQLi Database

You need to use VALUES() with comma separation for multiple insert or batch insert at a time. Use the following syntax that does not produce an invalid MySQL query on insert. The syntax is as follows:

INSERT INTO yourTableName VALUES(yourValue1),(yourValue1),(yourValue2),(yourValue3),(yourValue4),(yourValue5),.......N;

To understand the above syntax, let us create a table. The query to create a table is as follows:

mysql> create table addMultipleValues
   -> (
   -> Counter int NOT NULL
   -> );
Query OK, 0 rows affected (0.60 sec)

Now you can insert batch records in the table using VALUES() with comma separation. The query to insert record is as follows:

mysql> insert into addMultipleValues values(1),(2),(3),(4),(5),(6),(7),(8),(9),(10);
Query OK, 10 rows affected (0.27 sec)
Records: 10 Duplicates: 0 Warnings: 0

Now display all records from the table using select statement. The query is as follows:

mysql> select *from addMultipleValues;

The following is the output:

+---------+
| Counter |
+---------+
|       1 |
|       2 |
|       3 |
|       4 |
|       5 |
|       6 |
|       7 |
|       8 | 
|       9 |
|      10 |
+---------+
10 rows in set (0.00 sec)
raja
Published on 25-Feb-2019 10:47:40
Advertisements