How the ROW_COUNT() function works in Mariadb?

The ROW_COUNT() function in MariaDB returns the number of rows affected by the previous statement.

Posted on

The ROW_COUNT() function in MariaDB returns the number of rows affected by the previous statement. This can be used to track the number of rows inserted, updated, or deleted by a statement.

Syntax

The ROW_COUNT() function has the following syntax:

ROW_COUNT()

The ROW_COUNT() function does not take any arguments.

Examples

The following examples demonstrate how to use the ROW_COUNT() function:

Example 1: Counting the number of rows inserted

The following example shows how to use the ROW_COUNT() function to count the number of rows inserted into a table:

INSERT INTO `table` (`column1`, `column2`) VALUES ('value1', 'value2');

SELECT ROW_COUNT();

The output of the above query is:

1

This indicates that one row was inserted into the table table.

Example 2: Counting the number of rows updated

The following example shows how to use the ROW_COUNT() function to count the number of rows updated in a table:

UPDATE `table` SET `column1` = 'value1' WHERE `column2` = 'value2';

SELECT ROW_COUNT();

The output of the above query is:

1

This indicates that one row was updated in the table table.

Example 3: Counting the number of rows deleted

The following example shows how to use the ROW_COUNT() function to count the number of rows deleted from a table:

DELETE FROM `table` WHERE `column1` = 'value1';

SELECT ROW_COUNT();

The output of the above query is:

1

This indicates that one row was deleted from the table table.

The following functions are related to the ROW_COUNT() function:

  • FOUND_ROWS(): The FOUND_ROWS() function returns the number of rows that matched a SELECT statement, even if the rows were not returned due to the use of a LIMIT clause.
  • LAST_INSERT_ID(): The LAST_INSERT_ID() function returns the ID of the last row that was inserted into a table.

Conclusion

The ROW_COUNT() function is a useful tool for tracking the number of rows affected by a statement. This can be used to ensure that the correct number of rows were affected by a statement, or to track the progress of a long-running operation.