mysql batch insert insert statement

There are several ways to batch insert data in MySQL. Below I will introduce two of the commonly used methods:

Method 1: Multi-value insertion using INSERT INTO...VALUES statement

This is a simple way to insert multiple values ​​at once. Here is an example:

INSERT INTO your_table_name (column1, column2, column3)
VALUES
(value1, value2, value3),
(value4, value5, value6),
(value7, value8, value9);


In the above example, you need to replace your_table_name with your table name, column name and corresponding value. You can insert multiple rows of data at once.

Method 2: Use INSERT INTO…SELECT statement

Another way to bulk insert data is to use the INSERT INTO... SELECT statement, which allows you to select multiple rows of data from a query and insert them into the target table. Here is an example

INSERT INTO your_table_name (column1, column2, column3)
SELECT value1, value2, value3 FROM source_table WHERE condition;


In the above example, you need to replace your_table_name with the target table name, column1, column2, column3 with the column names of the target table, source_table with the source table name, and condition with the conditions for which data you want to select from the source table.

Using one of these two methods, you can batch insert multiple rows of data into a MySQL database. Note that for large batches of data inserted, you may need to consider performance issues, such as using transactions to improve the efficiency and data integrity of the insert operation.

Guess you like

Origin blog.csdn.net/nj0128/article/details/132708351