
- 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
Select some data from a database table and insert into another table in the same database with MySQL
To insert data from a table to another, use INSERT INTO statement. Let us first create a table −
mysql> create table DemoTable1 ( Id int, FirstName varchar(20), Age int ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1 values(101,'Chris',24); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1 values(102,'David',28); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1;
This will produce the following output −
+------+-----------+------+ | Id | FirstName | Age | +------+-----------+------+ | 101 | Chris | 24 | | 102 | David | 28 | +------+-----------+------+ 2 rows in set (0.00 sec)
Here is the query to create second table.
pre class="prettyprint notranslate" > mysql> create table DemoTable2 ( EmployeeId int, EmployeeName varchar(20), EmployeeAge int ); Query OK, 0 rows affected (0.00 sec)
Following is the query to select some data from one database table and insert into another table in the same database −
mysql> insert into DemoTable2(EmployeeId,EmployeeName,EmployeeAge) select Id,FirstName,Age from DemoTable1 where Id=102; Query OK, 1 row affected (0.00 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 −
+------------+--------------+-------------+ | EmployeeId | EmployeeName | EmployeeAge | +------------+--------------+-------------+ | 102 | David | 28 | +------------+--------------+-------------+ 1 row in set (0.00 sec)
- Related Articles
- MySQL statement to copy data from one table and insert into another table
- Fetch maximum ID value from the first table and insert to all the IDs in another table with MySQL INSERT INTO select?
- Insert values in a table by MySQL SELECT from another table in MySQL?
- MySQL INSERT INTO SELECT into a table with AUTO_INCREMENT
- How to insert data into a MySQL database with Java?
- MySQL INSERT INTO SELECT resulting in multiple rows inserted at once from another table
- Insert data from one table to another in MySQL?
- How to copy a table from one MySQL database to another?
- MySQL query to insert data from another table merged with constants?
- How to SELECT fields from one table and INSERT to another in MySQL?
- MySQL query for INSERT INTO using values from another table?
- How to get username using ID from another table in MySQL database?
- Get the last record from a table in MySQL database with Java?
- How do I INSERT INTO from one MySQL table into another table and set the value of one column?
- MySQL trigger to insert row into another table?

Advertisements