
- 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
Insert from one table with different structure to another in MySQL?
For this, use INSERT INTO SELECT statement. Let us first create a table −
mysql> create table DemoTable1 -> ( -> PersonId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> PersonName varchar(20), -> PersonAge int, -> PersonCountryName varchar(20) -> ); Query OK, 0 rows affected (0.55 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1(PersonName,PersonAge,PersonCountryName) values('Chris Brown',24,'US'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1(PersonName,PersonAge,PersonCountryName) values('John Doe',26,'UK'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable1(PersonName,PersonAge,PersonCountryName) values('David Miller',23,'AUS'); Query OK, 1 row affected (0.10 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1;
This will produce the following output −
+----------+--------------+-----------+-------------------+ | PersonId | PersonName | PersonAge | PersonCountryName | +----------+--------------+-----------+-------------------+ | 1 | Chris Brown | 24 | US | | 2 | John Doe | 26 | UK | | 3 | David Miller | 23 | AUS | +----------+--------------+-----------+-------------------+ 3 rows in set (0.00 sec)
Here is the query to create second table −
mysql> create table DemoTable2 -> ( -> EmployeeId int, -> EmployeeFullName varchar(30), -> EmployeeAge int, -> EmployeeCountryName varchar(20), -> EmployeeSalary int default 20000 -> ); Query OK, 0 rows affected (0.52 sec)
Here is the query to insert from one table with different structure to another −
mysql> insert into DemoTable2(EmployeeId,EmployeeFullName,EmployeeAge,EmployeeCountryName) select PersonId,PersonName,PersonAge,PersonCountryName from DemoTable1; Query OK, 3 rows affected (0.26 sec) Records: 3 Duplicates: 0 Warnings: 0
Display all records from the table using select statement −
mysql> select * from DemoTable1;
This will produce the following output −
+------------+------------------+-------------+---------------------+----------------+ | EmployeeId | EmployeeFullName | EmployeeAge | EmployeeCountryName | EmployeeSalary | +------------+------------------+-------------+---------------------+----------------+ | 1 | Chris Brown | 24 | US | 20000 | | 2 | John Doe | 26 | UK | 20000 | | 3 | David Miller | 23 | AUS | 20000 | +------------+------------------+-------------+---------------------+----------------+ 3 rows in set (0.00 sec)
- Related Articles
- Insert data from one table to another in MySQL?
- MySQL query to copy records from one table to another with different columns
- MySQL statement to copy data from one table and insert into another table
- How to SELECT fields from one table and INSERT to another in MySQL?
- Take all records from one MySQL table and insert it to another?
- MySQL query to insert data from another table merged with constants?
- Insert data from one schema to another in MySQL?
- How to insert values from one table into another in PostgreSQL?
- Insert values in a table by MySQL SELECT from another table in MySQL?
- Move rows from one table to another in MySQL?
- How do I INSERT INTO from one MySQL table into another table and set the value of one column?
- Fetch maximum ID value from the first table and insert to all the IDs in another table with MySQL INSERT INTO select?
- MySQL query for INSERT INTO using values from another table?
- MySQL trigger to insert row into another table?
- How to copy rows from one table to another in MySQL?

Advertisements