I have two similar tables in different databases, I want to insert into one from the other one. At the same time I want to ensure that every time mySql encounters a ‘duplicate id’ error it inserts the row at the bottom of the new table and assigns a new unique id. I have tried:INSERT...SELECT...ON DUPLICATE KEY UPDATE
But I can’t find a way to get it to insert into a new row if it finds ‘duplicate keys’ instead of updating the previous row.
I have two similar tables in different databases, I want to insert into one
Share
If you can assign new
idto every record being copied to the destination table, irrespective of whether theidin the source table existed previously in the destination or not, you can simply not provide theidand allow MySQL to resort to the defaultNULLvalue that causes anauto_incrementvalue to be assigned:Otherwise, you will need to use something like
NULLIF()to setidexplicitly toNULLwhere it already exists, based on joining the tables together in theSELECT: