
- 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
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 Articles
- 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 multiple values in a temporary table with a single MySQL query?
- Copy values of one column to another using INSERT and SELECT in a single MySQL query
- Insert values in a table by MySQL SELECT from another table in MySQL?
- Update only a single value from a MySQL table where select from same table ordered in descending order?
- Insert with a Select query in MySQL
- MySQL SELECT from two tables with a single 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?
- Insert multiple rows in a single MySQL query
- 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?

Advertisements