
- 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
MySQL statement to copy data from one table and insert into another table
For this, you can use INSERT INTO….SELECT statement. Let us first create a table −
mysql> create table DemoTabe1 (Marks int); Query OK, 0 rows affected (0.66 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTabe1 values(68); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTabe1 values(89); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTabe1 values(99); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTabe1 values(39); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTabe1 values(49); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement −
mysql> select *from DemoTabe1;
This will produce the following output −
+-------+ | Marks | +-------+ | 68 | | 89 | | 99 | | 39 | | 49 | +-------+ 5 rows in set (0.00 sec)
Following is the query to create second table −
mysql> create table DemoTabe2 (Score int); Query OK, 0 rows affected (0.66 sec)
Here is the query for inserting data to another table −
mysql> insert into DemoTabe2(Score) select Marks from DemoTabe1; Query OK, 5 rows affected (0.16 sec) Records: 5 Duplicates: 0 Warnings: 0
Display all records from the table using select statement −
mysql> select *from DemoTabe2;
This will produce the following output. Now the same records are visible in the second table −
+-------+ | Score | +-------+ | 68 | | 89 | | 99 | | 39 | | 49 | +-------+ 5 rows in set (0.00 sec)
- Related Articles
- Insert data from one table to another in MySQL?
- Simplest way to copy data from one table to another new table in MySQL?
- How do I INSERT INTO from one MySQL table into another table and set the value of one column?
- Copy column values from one table into another matching IDs in MySQL
- Select some data from a database table and insert into another table in the same database with MySQL
- How to insert values from one table into another in PostgreSQL?
- How to copy a table from one MySQL database to another?
- How to copy rows from one table to another in MySQL?
- MySQL trigger to insert row into another table?
- Take all records from one MySQL table and insert it to another?
- Update data in one table from data in another table in MySQL?
- MySQL query for INSERT INTO using values from another table?
- MySQL query to insert data from another table merged with constants?
- How to SELECT fields from one table and INSERT to another in MySQL?
- Insert from one table with different structure to another in MySQL?

Advertisements