This article will introduce to you how to insert multiple records with one insert statement in mysql. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
Common way to write insert statements:
INSERT INTO items(name,city,price,number,picture) VALUES('耐克运动鞋','广州',500,1000,'003.jpg');
This method can only insert one piece of data at a time. If you want to insert multiple pieces of data, you have to call this multiple times. SQL statement means establishing connections with the database multiple times. But this will increase the load on the server, because every time the SQL server is executed, the SQL must be analyzed, optimized and other operations. Fortunately, MySQL provides another solution, which is to use an INSERT statement to insert multiple records. This is not standard SQL syntax, so it can only be used in MySQL.
How to write an INSERT statement to insert batch data:
INSERT INTO [表名]([列名],[列名]) VALUES ([列值],[列值])), ([列值],[列值])), ([列值],[列值]));
You can see that the difference from the original regular INSERT statement is only the arrangement of the added values after VALUES, between each record Is it so easy to separate them with commas in the English input method state?
Example:
INSERT INTO items(name,city,price,number,picture) VALUES ('耐克运动鞋','广州',500,1000,'003.jpg'), ('耐克运动鞋2','广州2',500,1000,'002.jpg');
In this way, two pieces of data are inserted at one time.
Suggestion:
In the program, when inserting batch data, it is best to use this method of inserting data at once through an INSERT statement. This prevents the program from establishing multiple connections to the database, thereby increasing the server load.
Related recommendations: "mysql tutorial"
The above is the detailed content of How to insert multiple records with one insert statement in mysql. For more information, please follow other related articles on the PHP Chinese website!