
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Perform multiple inserts with INSERT INTO SELECT and UNION in MySQL
To perform multiple inserts, the syntax is as follows −
insert into yourTableName(yourColumnName1,yourColumnName2,yourColumnName3,..N) select yourValue1 as yourColumnName1,yourValue2 as yourColumnName2,yourValue3 as yourColumnName3,......N union select yourValue1 as yourColumnName1,yourValue2 as yourColumnName2,yourValue3 as yourColumnName3,......N . . N
To understand the above syntax, let us create a table −
mysql> create table DemoTable1936 ( StudentId int, StudentName varchar(20), StudentCountryName varchar(20) ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1936(StudentId,StudentName,StudentCountryName) select 1001 as StudentId,'Chris' as StudentName,'US' as StudentCountryName union select 1002 as StudentId,'Robert' as StudentName,'UK' as StudentCountryName union select 1003 as StudentId,'David' as StudentName,'AUS' as StudentCountryName; Query OK, 3 rows affected (0.00 sec) Records: 3 Duplicates: 0 Warnings: 0
Display all records from the table using select statement −
mysql> select * from DemoTable1936;
This will produce the following output −
+-----------+-------------+--------------------+ | StudentId | StudentName | StudentCountryName | +-----------+-------------+--------------------+ | 1001 | Chris | US | | 1002 | Robert | UK | | 1003 | David | AUS | +-----------+-------------+--------------------+ 3 rows in set (0.00 sec)
- Related Articles
- MySQL INSERT INTO SELECT into a table with AUTO_INCREMENT
- MySQL INSERT INTO SELECT resulting in multiple rows inserted at once from another table
- Perform MySQL SELECT INTO user-defined variable
- Multiple Inserts for a single column in MySQL?
- Insert with a Select query in MySQL
- How to add static value while INSERT INTO with SELECT in a MySQL query?
- MySQL UNION SELECT and IN clause in a single query
- Select the table name as a column in a UNION select query with MySQL?
- Preserve select order within MySQL UNION?
- Combine INSERT, VALUES, and SELECT in MySQL
- MySQL select query with multiple WHERE?
- Select and insert values with preceding zeros in a MySQL table
- MySQL select and insert in two tables with a single query
- Fetch maximum ID value from the first table and insert to all the IDs in another table with MySQL INSERT INTO select?
- Select some data from a database table and insert into another table in the same database with MySQL

Advertisements