- 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
Insert values in a table by MySQL SELECT from another table in MySQL?
Fir this, use INSERT INTO SELECT statement. Let us first create a table −
mysql> create table DemoTable1 -> ( -> Id int, -> Name varchar(20), -> Age int -> ); Query OK, 0 rows affected (1.72 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1 values(100,'Chris',24); Query OK, 1 row affected (0.61 sec) mysql> insert into DemoTable1 values(101,'Adam',23); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1 values(102,'John',25); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1 values(103,'Carol',26); Query OK, 1 row affected (0.26 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1;
This will produce the following output −
+------+-------+------+ | Id | Name | Age | +------+-------+------+ | 100 | Chris | 24 | | 101 | Adam | 23 | | 102 | John | 25 | | 103 | Carol | 26 | +------+-------+------+ 4 rows in set (0.00 sec)
Here is the query to create second table −
mysql> create table DemoTable2 -> ( -> EmployeeId int, -> EmployeeFirstName varchar(20), -> EmployeeAge int -> ); Query OK, 0 rows affected (1.63 sec)
Here is the query to insert values in DemoTable2 from MySQL select from DemoTable1 −
mysql> insert into DemoTable2(EmployeeId,EmployeeFirstName,EmployeeAge) select Id,Name,Age from DemoTable1 where Id=101; Query OK, 1 row affected (0.17 sec) Records: 1 Duplicates: 0 Warnings: 0
Let us check the table records −
mysql> select * from DemoTable2;
Here is the query to create second table −
+------------+-------------------+-------------+ | EmployeeId | EmployeeFirstName | EmployeeAge | +------------+-------------------+-------------+ | 101 | Adam | 23 | +------------+-------------------+-------------+ 1 row in set (0.00 sec)
- Related Articles
- MySQL query for INSERT INTO using values from another table?
- How to SELECT fields from one table and INSERT to another in MySQL?
- Updating a MySQL table with values from another table?
- Select and insert values with preceding zeros in a MySQL table
- Select some data from a database table and insert into another table in the same database with MySQL
- Insert data from one table to another in MySQL?
- MySQL INSERT INTO SELECT resulting in multiple rows inserted at once from another table
- MySQL select query to select rows from a table that are not in another table?
- Insert values from the first table to the second table using two SELECT statements in a single MySQL query
- Fetch maximum ID value from the first table and insert to all the IDs in another table with MySQL INSERT INTO select?
- How do I select data from one table only where column values from that table match the column values of another table in MySQL?
- Insert from one table with different structure to another in MySQL?
- How can we update the values in one MySQL table by using the values of another MySQL table?
- MySQL statement to copy data from one table and insert into another table
- MySQL INSERT INTO SELECT into a table with AUTO_INCREMENT

Advertisements