
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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)
Advertisements