MySql - INCLUDE DUPLICATE KEY INSERTS

I understand that there is INSERT IGNORE and INSERT ... ON DUPLICATE KEY UPDATE . However, when there is a duplicate key, I would like to do INSERT in a temporary table in order to keep a record of the unique key that was broken so that I can display it to the user.

Is there any way to do ON DUPLICATE INSERT ? If this helps, I try to do volume insertion.

+7
source share
1 answer

With ON DUPLICATE KEY UPDATE you cannot insert another table - and there is no alternative function.

Two custom / alternative ways of doing this:

Something similar to this should work:

 CREATE TABLE insert_logs ( id int not null ); delimiter | CREATE TRIGGER insert_logs_trigger BEFORE INSERT ON your_table FOR EACH ROW BEGIN INSERT INTO insert_logs SET id = NEW.id; END; | 

To get a list of duplicates in a table, you can:

 SELECT id FROM insert_logs HAVING COUNT(id) > 1 GROUP BY id; 
+7
source

All Articles