©SQL :: Manager
HomeProgram InformationTechnical detailsFree downloadDonate for a cup of coffee
myway SQL Manager for MySQL and MariaDB, Download on Uptodown
SQL functionMySQLMariaDB

The ROW_COUNT SQL Function in MySQL and MariaDB - Number of rows affected

ROW_COUNTSyntax:ROW_COUNT()
Return value:INTEGER
Function type:Information function
Function Description

The ROW_COUNT() function returns the number of affected rows modified, inserted, or deleted by an SQL statement.

For statements with a result set, such as SELECT, SHOW, DESC or HELP , -1 will be returned even if the result set is empty.

For DML statements other than SELECT and for ALTER TABLE, the function returns the number of rows affected.< br> For DDL statements, including TRUNCATE, and other statements that do not return a result set, the function returns 0.

For UPDATE statements, affected rows are the number of rows that were actually modified.

If the CLIENT_FOUND_ROWS flag is specified when connecting to mysqld, the function returns the number of rows that match the WHERE clause.

With REPLACE, deleted lines are also counted. If REPLACE deletes a row and adds a row, the ROW_COUNT() function returns 2.

With INSERT ... ON DUPLICATE KEY, updated rows are counted twice. If INSERT adds a row and modifies a row, ROW_COUNT() returns 3.

Rows deleted by foreign keys or triggers are not counted.

For prepared statements, ROW_COUNT() should be called after EXECUTE, not after DEALLOCATE PREPARE.

After a CALL statement, the function returns the number of rows affected by the last statement in the procedure.

After INSERT DELAYED, ROW_COUNT() returns the number of rows the statemant tried to insert, not the number of successful writes.

SQL Examples for the ROW_COUNT function

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

DROP TABLE IF EXISTS `test_table`;
    
CREATE TABLE `test_table` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) DEFAULT NULL,
  `type` enum('First','Second','Third','Last'DEFAULT NULL,
  `num` int(11) NOT NULL DEFAULT 0,
  `price` decimal(10,2) NOT NULL DEFAULT 0.00,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`)
ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `test_table` (`id`,`name`,`type`,`num`,`price`VALUES 
(1,'Art 1','First',1,10.50),
(2,'Art 2','Second',3,12.50),
(3,'Art 3','Second',2,15.50),
(4,'Art 4','Third',5,25.50),
(5,'Art 5','Last',2,5.50),
(6,'Art 6','First',12,1.50),
(7,'Art 7','Third',20,1.75),
(8,'Art 8','Last',100,11.90);

select * from `test_table`;

update `test_table` set num=0 where `type`='First';

select row_count();

delete from `test_table` where `type`='First' or `type`='Last';

select row_count();

INSERT INTO `test_table` (`name`,`type`,`num`,`price`VALUES 
('Art 10','First',1,10.50),
('Art 11','Second',3,12.50);

select row_count();

replace INTO `test_table` (`name`,`type`,`num`,`price`VALUES 
('Art 10','First',1,10.50),
('Art 11','Second',3,12.50);

select row_count();

id
name
type
num
price
int(11)varchar(20) LATIN1enum LATIN1int(11)decimal(10,2)
1Art 1First110.50
2Art 2Second312.50
3Art 3Second215.50
4Art 4Third525.50
5Art 5Last25.50
6Art 6First121.50
7Art 7Third201.75
8Art 8Last10011.90
row_count()
bigint(21)
2
row_count()
bigint(21)
4
row_count()
bigint(21)
2
row_count()
bigint(21)
4
The examples were created with the MyWAY SQL manager: Download

How to use the ROW_COUNT() function in MySQL and MariaDB databases

In MySQL and MariaDB the ROW_COUNT() function is used to get the number of affected rows from a previous SQL statement, such as INSERT, REPLACE, UPDATE or DELETE, within the current session. The function requires no arguments and can be applied immediately after the execution of SQL statements that update, insert, or delete data to get the number of rows affected.

The ROW_COUNT() function can be used to check whether a change statement (INSERT, REPLACE, UPDATE, DELETE) affected any table rows. A value of 0 indicates no rows were affected, while a positive value indicates the number of data rows affected. Also, the function can be used in conditional statements or control flow logic to perform different actions based on the number of rows involved. The feature can also be used to log or record the number of rows affected for auditing purposes or to track the impact of data changes over time.

ROW_COUNT() returns only the number of affected rows for the last statement executed in the current session. When multiple statements are executed in succession, only the update count of the last statement is available.
RIGHTRLIKEROUNDROW_COUNTRPADRPAD_ORACLERTRIM

Further MySQL und MariaDB SQL Information functions

BENCHMARKBENCHMARK(Number, Expression)
More about BENCHMARK Function

BINLOG_GTID_POSBINLOG_GTID_POS()

CHARSETCHARSET(String)
More about CHARSET Function

COERCIBILITYCOERCIBILITY(String)
More about COERCIBILITY Function

COLLATIONCOLLATION(String)
More about COLLATION Function

CONNECTION_IDCONNECTION_ID()
More about CONNECTION_ID Function

CURRENT_USERCURRENT_USER()
More about CURRENT_USER Function

CURRENT_ROLECURRENT_ROLE()
More about CURRENT_ROLE Function

DATABASEDATABASE()
More about DATABASE Function

DECODE_HISTOGRAMDECODE_HISTOGRAM()

DEFAULTDEFAULT(Column)
More about DEFAULT Function

FOUND_ROWSFOUND_ROWS()
More about FOUND_ROWS Function

LAST_INSERT_IDLAST_INSERT_ID([Expression])
More about LAST_INSERT_ID Function

LAST_VALUELAST_VALUE(Expression [, Expression ...])
More about LAST_VALUE Function

ROWNUMROWNUM()

SCHEMASCHEMA()
More about SCHEMA Function

SESSION_USERSESSION_USER()
More about SESSION_USER Function

SYSTEM_USERSYSTEM_USER()
More about SYSTEM_USER Function

USERUSER()
More about USER Function

VERSIONVERSION()
More about VERSION Function

Numeric functionsString functionsRegular ExpressionsDate and Time functions
Comparison functionsEncryption & CompressionConversion functionsNULL functions
Aggregate functionsWindow functionsJSON functionsGeometric functions
Sequence functionsInformation functionsDynamic ColumnsMiscellaneous functions
More information about the ROW_COUNT SQL function: mysql.com and mariadb.com
Updated: 2023-09-17ImprintContactTerms & conditionsPrivacy & CookiesUpdatesSitemapFacebookLinkedinTwitterStatistics©2020-2024 Heino Cunze-Fischer