Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find out if REPLACE statement has replaced or just inserted in MySQL

Tags:

mysql

Is there a way in MySQL to find out if the last REPLACE statement actually replaced any row or just performed a regular insert? LAST_INSERT_ID() doesn't seem to help, since the autoincrement counter is updated in all cases.

like image 436
GOTO 0 Avatar asked Feb 17 '23 18:02

GOTO 0


2 Answers

You need to determine the affected rows count. REPLACE doesn't update rows; it inserts or deletes and then inserts.

From the MySQL manual:

The affected-rows count makes it easy to determine whether REPLACE only added a row or whether it also replaced any rows: Check whether the count is 1 (added) or greater (replaced).

Check out the manual page: MySQL REPLACE Syntax

like image 130
Brendan Bullen Avatar answered Feb 24 '23 20:02

Brendan Bullen


REPLACE returns the count of affected rows. This can be retrieved in SQL by ROW_COUNT().

like image 30
Joop Eggen Avatar answered Feb 24 '23 22:02

Joop Eggen