Inserting Multiple MySQL Records With One Query

November 3rd, 2010 - Posted by Steve Marks to MySQL, Web Development.

We can insert a single record into a MySQL table using the following format:

INSERT INTO `table_name` 
	(`column1`, `column2`, ...) 
VALUES 
	('value1', 'value2', ...)

But how do we go about inserting more than one record at the same time? The answer is simple and involves us duplicating the last part of the query the required amount of times. Let me show you an example:

INSERT INTO `table_name`
	(`column1`, `column2`, ...) 
VALUES 
	('value1', 'value2', ...), 
	('value1', 'value2', ...), 
	('value1', 'value2', ...)

By simply separating the sets of values with a comma we can now insert multiple records in one go. Much more efficient and cleaner than executing a separate INSERT command for each new record.

Tags:
This entry was posted on Wednesday, November 3rd, 2010 at 8:38 pm by +Steve Marks and is filed under MySQL, Web Development. You can follow any responses to this entry through the RSS 2.0 feed.
Comments...

Fear not, we won't publish this

Comments (0)

No comments have been left yet. Be the first