Thursday, April 30, 2009

MySQL multiple insert statements

If you need to insert multiple records into a MySQL database at once, you can perform each insert as a separate query or combine them into one query as follows. Suppose you want to insert 3 records into a users table. Here's the standard separate query method:
INSERT INTO users (name, email) VALUES ('Ed','ed@internet.com');
INSERT INTO users (name, email) VALUES ('Jon','jon@intertubes.net');
INSERT INTO users (name, email) VALUES ('Jamey','jameyd@web.org');

Or you could do this alternatively as one INSERT statement:
INSERT INTO users (name, email) VALUES 
('Ed','ed@internet.com'),
('Jon','jon@intertubes.net'),
('Jamey','jameyd@web.org');

No comments:

Post a Comment