

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 query for INSERT INTO using values from another table?
For this, use INSERT INTO SELECT statement.
Let us create a table −
Example
mysql> create table demo82 -> ( -> id int, -> name varchar(20) -> ); Query OK, 0 rows affected (2.06
Insert some records into the table with the help of insert command −
Example
mysql> insert into demo82 values(100,'John'); Query OK, 1 row affected (0.14 mysql> insert into demo82 values(101,'Bob'); Query OK, 1 row affected (0.32 mysql> insert into demo82 values(101,'David'); Query OK, 1 row affected (0.09 mysql> insert into demo82 values(101,'Mike'); Query OK, 1 row affected (0.12 mysql> insert into demo82 values(100,'Sam'); Query OK, 1 row affected (0.07
Display records from the table using select statement −
Example
mysql> select *from demo82;
This will produce the following output −
Output
+------+-------+
| id | name |+------+-------+
| 100 | John || 101 | Bob |
| 101 | David || 101 | Mike |
| 100 | Sam |+------+-------+
5 rows in set (0.00 sec)
Following is the query to create second table.
Example
mysql> create table demo83 -> ( -> id int, -> username varchar(20) -> ); Query OK, 0 rows affected (1.25
Insert some records into the second table with the help of insert command −
Example
mysql> insert into demo83(id,username) -> select id,name from demo82 where id=101; Query OK, 3 rows affected (0.14 sec) Records: 3 Duplicates: 0 Warnings: 0
Display records from the second table using select statement −
Example
mysql> select *from demo83;
This will produce the following output −
Output
+------+----------+
| id | username |+------+----------+
| 101 | Bob || 101 | David |
| 101 | Mike |+------+----------+
3 rows in set (0.00 sec)
- Related Questions & Answers
- How to insert values from one table into another in PostgreSQL?
- Insert values in a table by MySQL SELECT from another table in MySQL?
- MySQL trigger to insert row into another table?
- MySQL statement to copy data from one table and insert into another table
- MySQL query to insert data from another table merged with constants?
- Copy column values from one table into another matching IDs in MySQL
- Insert data from one table to another in MySQL?
- MySQL INSERT INTO SELECT resulting in multiple rows inserted at once from another table
- How do I INSERT INTO from one MySQL table into another table and set the value of one column?
- Updating a MySQL table with values from another table?
- Auto insert values into a MySQL table in a range?
- Insert JSON into a MySQL table?
- Insert values from the first table to the second table using two SELECT statements in a single MySQL query
- Select some data from a database table and insert into another table in the same database with MySQL
- Fetch maximum ID value from the first table and insert to all the IDs in another table with MySQL INSERT INTO select?
Advertisements