To merge two MySQL tables, use the following syntax -
INSERT IGNORE INTO yourTableName1 select *from yourTableName2;
We will create two tables containing some records. After that, the merge process will start using the above syntax.
Create the first table-
mysql> create table MergeDemo1 -> ( -> id int, -> primary key(id), -> Name varchar(200) -> ); Query OK, 0 rows affected (1.00 sec)
Insert records into the table-
mysql> insert into MergeDemo1 values(1,'John'); Query OK, 1 row affected (0.21 sec)
Display records in the table
mysql> select *from MergeDemo1;
The following is the first table The output of -
+----+------+ | id | Name | +----+------+ | 1 | John | +----+------+ 1 row in set (0.00 sec)
Now let us create the second table-
mysql> create table MergeDemo2 -> ( -> id int, -> primary key(id), -> Name varchar(200) -> ); Query OK, 0 rows affected (0.51 sec)
Insert the records in the second table-
mysql> insert into MergeDemo2 values(2,'David'); Query OK, 1 row affected (0.18 sec)
Display all the records in the second table Record -
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 the 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 we use the select statement to check whether the second table data is merged. The query is as follows -
mysql> select *from MergeDemo1;
This is the output showing the merged table -
+----+-------+ | id | Name | +----+-------+ | 1 | John | | 2 | David | +----+-------+ 2 rows in set (0.00 sec)
The above is the detailed content of How to merge two MySQL tables?. For more information, please follow other related articles on the PHP Chinese website!