We have a database that gets updated everyday at midnight with a cronjob, we get new data from an external XML.
What we do is that we insert all the new content and
You can get this information at the time of the insert/update by examining the number of affected rows in the result set.
MySQL documentation states:
With ON DUPLICATE KEY UPDATE, the affected-rows value per row is 1 if the row is inserted as a new row and 2 if an existing row is updated.
You'll need to combine ROW_COUNT with LAST_INSERT_ID to get your answer and insert one row at a time.
I can say How I did in PHP:
1) Simple query SELECT MAX(id) and remember it to $max_id from table before Insert On Duplicate.
2) Then during the update process collect ID of affected rows (no mater new or existed): $ids[] = mysql_insert_id();
3) Then $inserted_rows = max($ids)-$max_id;
4) Updated rows = count($ids_srt)-$inserted_rows
$max_id = mysql_query("SELECT MAX(id) from table");
$max_id = mysql_result($max_id, 0);
// !!! prepare here 'insert on duplicate' query in a cycle
$result=mysql_query($query);
$ids[] = mysql_insert_id();
// finish inserting and collecting affected ids and close cycle
$inserted_rows = max($ids)- $max_id;
$updated_rows = count($ids)- $inserted_rows
Add an update_count INT NOT NULL DEFAULT 1
column and change your query:
INSERT
INTO table (id, col1, col2, col3)
VALUES
(id_value, val1, val2, val3),
(id_value, val1, val2, val3,),
(id_value, val1, val2, val3),
(id_value, val1, val2, val3),
ON DUPLICATE KEY
UPDATE
col1 = VALUES (col1),
col2 = VALUES (col2),
col3 = VALUES (col3),
update_count = update_count + 1;
You can also increment it in a BEFORE UPDATE
trigger which will allow you to keep the query as is.