How to do a batch insert in MySQL

Genadinik picture Genadinik · Apr 3, 2011 · Viewed 269.4k times · Source

I have 1-many number of records that need to be entered into a table. What is the best way to do this in a query? Should I just make a loop and insert one record per iteration? Or is there a better way?

Answer

nietaki picture nietaki · Apr 3, 2011

From the MySQL manual

INSERT statements that use VALUES syntax can insert multiple rows. To do this, include multiple lists of column values, each enclosed within parentheses and separated by commas. Example:

INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);