

- 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
A single MySQL query to select value from first table and insert in the second?
Let us first create a table −
mysql> create table DemoTable1( Value int ); Query OK, 0 rows affected (0.65 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1 values(67); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1 values(46); Query OK, 1 row affected (0.11 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable1;
This will produce the following output −
+-------+ | Value | +-------+ | 67 | | 46 | +-------+ 2 rows in set (0.00 sec)
Following is the query to create the second table −
mysql> create table DemoTable2( Score int ); Query OK, 0 rows affected (0.70 sec)
Following is how you can insert record in the second table from the first table −
mysql> insert into DemoTable2(Score) select Value from DemoTable1; Query OK, 2 rows affected (0.13 sec) Records: 2 Duplicates: 0 Warnings: 0
Display all records from the table using select statement −
mysql> select *from DemoTable2;
This will produce the following output −
+-------+ | Score | +-------+ | 67 | | 46 | +-------+ 2 rows in set (0.00 sec)
- Related Questions & Answers
- Insert values from the first table to the second table using two SELECT statements in a single MySQL query
- A single MySQL query to insert records (not all) in the second table from the first table
- Fetch maximum ID value from the first table and insert to all the IDs in another table with MySQL INSERT INTO select?
- MySQL select and insert in two tables with a single query
- Implement MySQL trigger in the first table to insert records in the second table?
- Insert values in a table by MySQL SELECT from another table in MySQL?
- Insert multiple values in a temporary table with a single MySQL query?
- MySQL SELECT from two tables with a single query
- Insert with a Select query in MySQL
- How can I update a field in a MySQL database table by adding a value in the second table with a value from the first table?
- Update only a single value from a MySQL table where select from same table ordered in descending order?
- Copy values of one column to another using INSERT and SELECT in a single MySQL query
- Select the minimum value from the maximum values of two tables with a single MySQL query?
- MySQL select query to select rows from a table that are not in another table?
- How to SELECT fields from one table and INSERT to another in MySQL?
Advertisements