- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MySQL INSERT INTO SELECT resulting in multiple rows inserted at once from another table
Let us first create a table −
mysql> create table DemoTable(Name varchar(100)); Query OK, 0 rows affected (0.55 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1 values('Chris'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1 values('Robert'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1 values('Adam'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1 values('Bob'); Query OK, 1 row affected (0.20 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable1;
This will produce the following output −
+--------+ | Name | +--------+ | Chris | | Robert | | Adam | | Bob | +--------+ 4 rows in set (0.00 sec)
Following is the query to create second table.
mysql> create table DemoTable2(ListOfEmployee text); Query OK, 0 rows affected (0.59 sec)
Following is the query to perform INSERT INTO SELECT resulting in multiple rows inserted −
mysql> insert into DemoTable2(ListOfEmployee) select group_concat(Name) from DemoTable1; Query OK, 1 row affected (0.20 sec) Records − 1 Duplicates − 0 Warnings − 0
Display all records from the table using select statement −
mysql> select *from DemoTable2;
This will produce the following output −
+-----------------------+ | ListOfEmployee | +-----------------------+ | Chris,Robert,Adam,Bob | +-----------------------+ 1 row in set (0.00 sec)
- Related Articles
- Insert multiple rows from another table but the inserted records should be distinct
- Insert values in a table by MySQL SELECT from another table in MySQL?
- Can I insert two or more rows in a MySQL table at once?
- Select some data from a database table and insert into another table in the same database with MySQL
- MySQL select query to select rows from a table that are not in another table?
- Fetch maximum ID value from the first table and insert to all the IDs in another table with MySQL INSERT INTO select?
- MySQL query for INSERT INTO using values from another table?
- MySQL INSERT INTO SELECT into a table with AUTO_INCREMENT
- MySQL statement to copy data from one table and insert into another table
- How to SELECT fields from one table and INSERT to another in MySQL?
- MySQL trigger to insert row into another table?
- Perform MySQL SELECT on dates inserted into the table as VARCHAR values
- Perform multiple inserts with INSERT INTO SELECT and UNION in MySQL
- Insert data from one table to another in MySQL?
- How do I INSERT INTO from one MySQL table into another table and set the value of one column?

Advertisements