I have a procedure that contains a INSERT ..... ON DUPLICATE KEY UPDATE.
If there a way in MySQL to determine the total rows that were inserted and the total rows that were replaced?
Thanks
I have a procedure that contains a INSERT ..... ON DUPLICATE KEY UPDATE.
If there a way in MySQL to determine the total rows that were inserted and the total rows that were replaced?
Thanks
You can use http://dev.mysql.com/doc/refman/5.0/en/mysql-affected-rows.html after the query.
For INSERT ... ON DUPLICATE KEY UPDATE statements, the affected-rows value is 1 if the row is inserted as a new row and 2 if an existing row is updated
You can calculate this using "Records" and "Duplicates". Records
is the total number of rows affected. Duplicates
is the number of updates. So Records - Duplicates
is the number of inserted rows.
For example:
mysql> insert into your_table (...)
-> select ... from your_other_table
-> on duplicate key update ...;
Query OK, 3 rows affected (0.00 sec)
Records: 2 Duplicates: 1 Warnings: 0
That means that 1 new row was inserted and 1 existing row was updated.