multiple insert mysql code example

Example 1: mysql best way to insert many rows

INSERT INTO table_name(id,colname) values(1,"row 1"),(2, "row 2"),...;

Example 2: mysql insert into multiple tables

BEGIN;
INSERT INTO users (username, password)
  VALUES('test', 'test');
INSERT INTO profiles (userid, bio, homepage) 
  VALUES(LAST_INSERT_ID(),'Hello world!', 'http://www.stackoverflow.com');
COMMIT;

Example 3: insert multiple items in mysql

#Assuming that there is a database called MyDatabase
#The following adds items to a table called TableName with columns No., Age, and class
use MyDatabase;
insert into TableName(No., Name, Age, Class)
values(1, "VinCoD", 21, "4E");

Example 4: mysql insert multiple rows based on select

INSERT INTO table1 (col1, col2, col3, col4, col5) 
SELECT col1,col2,col3,col4,col5 
FROM table2 t2 
WHERE t2.val2 IN (MULTIPLE VALUES) 
AND (Another Conditional);

Tags:

Sql Example