How to know if when using “on duplicate key update” a row was inserted or updated?

后端 未结 3 1285
-上瘾入骨i
-上瘾入骨i 2020-12-09 15:09

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

3条回答
  •  醉梦人生
    2020-12-09 15:53

    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.

提交回复
热议问题