Source: mysqlpub.com
MySQL has two functions to calculate the number of rows affected by the previous statement. Unlike SqlServer/Oracle, do not cause functional problems due to this difference:
1. Use the found_rows () function to determine the number of rows obtained by the Select statement.
2. Use the row_count () function to determine the number of rows affected by Update or Delete. Note that if the values before and after Update are the same, row_count is 0, unlike SQL Server's @ rowcount or Oracle's rowcount, as long as the row is updated, the number of affected rows will be greater than 0, regardless of whether the field value changes before and after update.
Example:
MySQL test (database version: 5.1.30 ):
1. Create a database table:
Copy codeThe Code is as follows:
Create table t (
Id int,
Name varchar (50 ),
Address varchar (100 ),
Primary key (id, name)
) Engine = InnoDB;
2. Insert test data:
Copy codeThe Code is as follows:
Insert into t
(Id, name, address)
Values
(1, 'yubowei', 'weifang '),
(2, 'Sam ', 'qingdao ');
3. Update Test
Copy codeThe Code is as follows:
Update t
Set address = 'weifang'
Where id = 1
And name = 'yubowei ';
Check the number of affected rows:
Select row_count (); => the execution result is 0;
4. test again
Copy codeThe Code is as follows:
Update t
Set address = 'beijing'
Where id = 1
And name = 'yubowei ';
Check the number of affected rows:
Select row_count (); => the execution result is 1;
From the test above, we can conclude that in MySQL, only when the record is modified, row_count will record the number of affected rows. Otherwise, if the record exists but there is no actual Modification
The update record is not included in row_count.
This is different from SQL ROWCOUNT in oracle.
Test on ORACLE (database version: 10 Gb ):
1. Create a database table:
Copy codeThe Code is as follows:
Create table t (
Id int,
Name varchar2 (50 ),
Address varcharacter 2 (100 ),
Primary key (id, name)
);
2. Insert test data:
Copy codeThe Code is as follows:
Insert into t
(Id, name, address)
Values
(1, 'yubowei', 'weifang '),
(2, 'Sam ', 'qingdao ');
3. Update Test
Copy codeThe Code is as follows:
Update t
Set address = 'weifang'
Where id = 1
And name = 'yubowei ';
Check the number of affected rows:
V_RowCount: = SQL % ROWCOUNT; => the execution result is 1;
4. test again
Copy codeThe Code is as follows:
Update t
Set address = 'beijing'
Where id = 1
And name = 'yubowei ';
Check the number of affected rows:
V_RowCount: = SQL % ROWCOUNT; => the execution result is 1;
From the test above, we can see that in ORACLE, as long as the updated record exists, no matter the actual data is not modified, the number of affected rows will be accumulated and recorded.
Note: currently, no parameter can be set for row_count. If necessary, it can only be implemented in other ways.