Tags
Asked 3 years ago
17 Jun 2021
Views 305
Esteban

Esteban posted

Inserting multiple rows in mysql

Inserting multiple rows in mysql
sarah

sarah
answered Apr 28 '23 00:00

To insert multiple rows in MySQL, you can use the INSERT INTO statement with multiple VALUES clauses, each containing a set of values to be inserted. Here's an example:



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

In this example, you specify the table name and the columns to be inserted into using the INSERT INTO statement. Then, you list multiple sets of values to be inserted using the VALUES keyword, separated by commas and enclosed in parentheses.

Each set of values corresponds to a row to be inserted into the table. The order of the values should match the order of the columns specified in the INSERT INTO statement.

You can also use the SELECT statement to insert multiple rows into a table based on the results of a query. Here's an example:

INSERT INTO mytable (column1, column2, column3)
SELECT column1, column2, column3
FROM othertable
WHERE condition = 'value';

In this example, you use the SELECT statement to select multiple rows from another table based on a condition and insert the results into mytable . The columns in the SELECT statement should match the columns specified in the INSERT INTO statement.

Note that when inserting multiple rows at once, the number of rows you can insert in a single query may be limited by the maximum packet size for your database server. If you need to insert a large number of rows, you may want to split them into smaller batches or use a different approach, such as a bulk data loading tool.
Post Answer