
- 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
How to copy rows from one table to another in MySQL?
For this, use INSERT INTO SELECT statement. Let us first create a table −
mysql> create table DemoTable1879 ( Id int, Name varchar(20) ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1879 values(101,'Chris Brown'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1879 values(102,'David Miller'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1879 values(103,'Adam Smith'); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1879;
This will produce the following output −
+------+--------------+ | Id | Name | +------+--------------+ | 101 | Chris Brown | | 102 | David Miller | | 103 | Adam Smith | +------+--------------+ 3 rows in set (0.00 sec)
Here is the query to create second table −
mysql> create table DemoTable1880 ( ClientId int, ClientName varchar(20) ); Query OK, 0 rows affected (0.00 sec)
Here is the query to copy rows from one table to another −
mysql> insert into DemoTable1880(ClientId,ClientName) select Id,Name from DemoTable1879 where Id IN(101,103); Query OK, 2 rows affected (0.00 sec) Records: 2 Duplicates: 0 Warnings: 0
Display all records from the table using select statement −
mysql> select * from DemoTable1880;
This will produce the following output −
+----------+-------------+ | ClientId | ClientName | +----------+-------------+ | 101 | Chris Brown | | 103 | Adam Smith | +----------+-------------+ 2 rows in set (0.00 sec)
- Related Articles
- How to copy a table from one MySQL database to another?
- Move rows from one table to another in MySQL?
- Copy all rows of a table to another table in MySQL?
- Simplest way to copy data from one table to another new table in MySQL?
- MySQL statement to copy data from one table and insert into another table
- MySQL query to copy records from one table to another with different columns
- Copy column values from one table into another matching IDs in MySQL
- How to copy tables or databases from one MySQL server to another MySQL server?
- Copy a few columns from a table to another in MySQL
- How to copy data from one field to another on every row in MySQL?
- Insert data from one table to another in MySQL?
- Copy from one column to another (different tables same database) in MySQL?
- How to SELECT fields from one table and INSERT to another in MySQL?
- How to copy a collection from one database to another in MongoDB?
- How to copy files from one folder to another using Python?

Advertisements