Macilias
Macilias

Reputation: 3543

Go: sql RowsAffected with ON DUPLICATE KEY UPDATE

I struggle to find some proper documentation which describes the semantic of the RowsAffected value other than that:

RowsAffected returns the number of rows affected by an update, insert, or delete. Not every database or database driver may support this.

I'm using a query with ON DUPLICATE KEY UPDATE and the value of RowsAffected is 2 after an update. Is it because it reduces to execution to delete and insert?

The server is an MySql server Ver 8.0.12 for osx10.13 on x86_64

Upvotes: 0

Views: 1142

Answers (1)

Paul Spiegel
Paul Spiegel

Reputation: 31812

mysql_affected_rows() is the equivalent of the SQL function ROW_COUNT().

For INSERT .. ON DUPLICATE KEY UPDATE the documentation states:

For INSERT ... ON DUPLICATE KEY UPDATE statements, 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. If you specify the CLIENT_FOUND_ROWS flag, the affected-rows value is 1 (not 0) if an existing row is set to its current values.

Upvotes: 2

Related Questions