
- 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 can I merge two MySQL tables?
To merge two MySQL tables, use the following syntax −
INSERT IGNORE INTO yourTableName1 select *from yourTableName2;
We will create two tables with some records. After that the merge process will begin using the above syntax.
Creating first table −
mysql> create table MergeDemo1 -> ( -> id int, -> primary key(id), -> Name varchar(200) -> ); Query OK, 0 rows affected (1.00 sec)
Inserting records into the table −
mysql> insert into MergeDemo1 values(1,'John'); Query OK, 1 row affected (0.21 sec)
Displaying records from the table
mysql> select *from MergeDemo1;
The following is the output of the first table −
+----+------+ | id | Name | +----+------+ | 1 | John | +----+------+ 1 row in set (0.00 sec)
Let us now create second table −
mysql> create table MergeDemo2 -> ( -> id int, -> primary key(id), -> Name varchar(200) -> ); Query OK, 0 rows affected (0.51 sec)
Inserting records in the second table −
mysql> insert into MergeDemo2 values(2,'David'); Query OK, 1 row affected (0.18 sec)
Displaying all records from second table −
mysql> select *from MergeDemo2;
The following is the output of the second table −
+----+-------+ | id | Name | +----+-------+ | 2 | David | +----+-------+ 1 row in set (0.00 sec)
The following is the query to merge two tables.
mysql> INSERT IGNORE -> INTO MergeDemo1 select *from MergeDemo2; Query OK, 1 row affected (0.19 sec) Records: 1 Duplicates: 0 Warnings: 0
Now, let us check whether the second table data is merged or not with the help of select statement. The query is as follows −
mysql> select *from MergeDemo1;
Here is the output that displays the merged table −
+----+-------+ | id | Name | +----+-------+ | 1 | John | | 2 | David | +----+-------+ 2 rows in set (0.00 sec)
- Related Articles
- Merge two tables with union in MySQL?
- How can we compare data in two MySQL tables?
- How to add/merge two hash tables in PowerShell?
- How can I merge properties of two JavaScript objects dynamically?
- How can I sum columns across multiple tables in MySQL?
- How can I change the default sort order of MySQL tables?
- How can I see the description of a MySQL Temporary Tables?
- How can we merge two Python dictionaries?
- MySQL join two tables?
- How can I the date of creation and updation of tables in MySQL?
- How can you perform inner join on two tables using MySQL in Python?
- How can you perform left join on two tables using MySQL in Python?
- How can you perform right join on two tables using MySQL in Python?
- How can you perform full join on two tables using MySQL in Python?
- How can you perform self join on two tables using MySQL in Python?

Advertisements