Getting number of rows inserted for ON DUPLICATE KEY UPDATE multiple insert?

The number of inserts would be 2000 minus the number of affected rows. More generally:

(numberOfValuesInInsert * 2) - mysql_affected_rows()

EDIT:

As tomas points out, The MySQL docs actually say:

With ON DUPLICATE KEY UPDATE, the affected-rows value per row is 1 if the row is inserted as a new row, 2 if an existing row is updated, and 0 if an existing row is set to its current values.

[emphasis mine]

Consequently, if setting an existing row to the same values is a possibility, it’s impossible to tell how many rows were updated vs. inserted, since two inserts would be indistinguishable from one update with different values + one update with the same values.

Leave a Comment