Count rows affected by the last statement. This function returns the number of rows inserted, updated, or deleted by the previous DML operation.
ROW_COUNT() returns the number of rows updated, inserted or deleted by the preceding statement. This is the same as the row count that the mariadb client displays and the value from the C API function.
Generally:
For statements which return a result set (such as , , or ), returns -1, even when the result set is empty. This is also true for administrative statements, such as .
For DML statements other than and for , returns the number of affected rows.
For DDL statements (including ) and for other statements which don't return any result set (such as , , or ), returns 0.
For , affected rows is by default the number of rows that were actually changed. If the CLIENT_FOUND_ROWS flag to is specified when connecting to mariadbd, affected rows is instead the number of rows matched by the WHERE clause.
For , deleted rows are also counted. So, if REPLACE deletes a row and adds a new row, ROW_COUNT() returns 2.
For , values returned are as follows:
0: an existing row is set to its current values, and the CLIENT_FOUND_ROWS is not set.
1: the values are inserted as a new row, or an existing row is set to its current values, and the CLIENT_FOUND_ROWS is set.
2: an existing row is updated with new values.
ROW_COUNT() does not take into account rows that are not directly deleted/updated by the last statement. This means that rows deleted by foreign keys or triggers are not counted.
Warning: You can use ROW_COUNT() with prepared statements, but you need to call it after EXECUTE, not after , because the row count for allocate prepare is always 0.
Warning: When used after a statement, this function returns the number of rows affected by the last statement in the procedure, not by the whole procedure.
Warning: After , ROW_COUNT() returns the number of the rows you tried to insert, not the number of the successful writes.
This information can also be found in the .
Statements using the ROW_COUNT() function are not .
Example with prepared statements:
This page is licensed: GPLv2, originally from
ROW_COUNT()CREATE TABLE t (A INT);
INSERT INTO t VALUES(1),(2),(3);
SELECT ROW_COUNT();
+-------------+
| ROW_COUNT() |
+-------------+
| 3 |
+-------------+
DELETE FROM t WHERE A IN(1,2);
SELECT ROW_COUNT();
+-------------+
| ROW_COUNT() |
+-------------+
| 2 |
+-------------+SET @q = 'INSERT INTO t VALUES(1),(2),(3);';
PREPARE stmt FROM @q;
EXECUTE stmt;
Query OK, 3 rows affected (0.39 sec)
Records: 3 Duplicates: 0 Warnings: 0
SELECT ROW_COUNT();
+-------------+
| ROW_COUNT() |
+-------------+
| 3 |
+-------------+