Copying Rows with Duplicate Handling in Identical Schema Tables
You want to transfer rows from table 'srcTable' to 'destTable' while preventing duplicates. However, using a subquery in a WHERE clause hasn't worked effectively for you.
Resolving the Issue with WHERE Clauses
The error lies in the absence of a proper clause in the subquery to identify duplicates. Add the necessary conditions in your subquery as follows:
INSERT INTO destTable SELECT Field1, Field2, Field3, ... FROM srcTable WHERE NOT EXISTS(SELECT * FROM destTable WHERE (srcTable.Field1 = destTable.Field1 AND srcTable.Field2 = destTable.Field2 ... etc.) )
Alternative Approach Using Outer Join
Alternatively, consider using an outer join for a more concise solution:
INSERT INTO destTable SELECT s.field1, s.field2, s.field3, ... FROM srcTable s LEFT JOIN destTable d ON (d.Key1 = s.Key1 AND d.Key2 = s.Key2 AND...) WHERE d.Key1 IS NULL
Handling Duplicates in the Source Table
If your concern lies specifically with duplicate rows in the source table, consider using a distinct clause in your insert statement:
INSERT INTO destTable SELECT Distinct field1, field2, field3, ... FROM srcTable
Additional Recommendations
The above is the detailed content of How to Efficiently Copy Rows from One Table to Another While Preventing Duplicates?. For more information, please follow other related articles on the PHP Chinese website!