Description of mysqlfound_row () and row_count () instances

Source: Internet
Author: User
Tags sql rowcount
The mysqlfound_row () and row_count () functions are used to calculate the number of rows affected by the previous statement. The difference is that found_row is used to obtain the number of rows affected by the Select statement, while row_count is used to obtain the number of rows affected by the Update or Delete. 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:

create table t(id int,name varchar(50),address varchar(100),primary key(id,name))engine =InnoDB;

2. Insert test data:

insert into t  (id,name,address)values  (1,'yubowei','weifang'),  (2,'sam','qingdao');

3. Update test

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

update tset address = 'beijing'where id = 1and 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:

create table t(id int,name varchar2(50),address varchar2(100),primary key(id,name));

2. Insert test data:

insert into t(id,name,address)values(1,'yubowei','weifang'),(2,'sam','qingdao');

3. Update test

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

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.