Home > Database > Mysql Tutorial > body text

How to insert multiple records with one insert statement in mysql

醉折花枝作酒筹
Release: 2021-05-25 09:21:02
forward
7472 people have browsed it

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.

How to insert multiple records with one insert statement in mysql

Common way to write insert statements:

INSERT INTO items(name,city,price,number,picture)  VALUES('耐克运动鞋','广州',500,1000,'003.jpg');
Copy after login

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

([列值],[列值])),

([列值],[列值])),

([列值],[列值]));
Copy after login

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');
Copy after login

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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template