Merge MySQL tables with similar structures
There are many ways to merge two MySQL tables with similar structures. One approach involves resolving potential primary key conflicts.
Method 1: INSERT IGNORE
If preserving existing rows in table_1 is critical, using the following query will maintain the integrity of table_1 data while merging new rows from table_2:
<code class="language-sql">INSERT IGNORE INTO table_1 SELECT * FROM table_2 ;</code>
This query ignores any rows in table_2 that have the same primary key as already exist in table_1 and only inserts rows with a unique primary key.
Method 2: REPLACE
For scenarios where you need to update existing rows in table_1 with data from table_2, the following query applies:
<code class="language-sql">REPLACE INTO table_1 SELECT * FROM table_2 ;</code>
This query will replace the matching rows in table_1 with the corresponding rows in table_2 while inserting the row with the unique primary key.
The above is the detailed content of How Can I Merge Two MySQL Tables with Similar Structures?. For more information, please follow other related articles on the PHP Chinese website!