Explanation and usage of found_row () and Row_count () in MySQL [Copy link]

Source: Internet
Author: User
Tags sql rowcount rowcount

Kider accessible by lift 1#  Posted in 2010-8-31 16:06:04 | Just look at the author | Reverse View | Reading mode
 Source: mysqlpub.com

there are two functions in MySQL to calculate how many rows were affected by the previous statement, unlike Sqlserver/oracle, and do not cause functional problems because of this difference:

1, judge the number of rows obtained by select with the Found_rows () function.

2, determine the number of rows affected by update or delete with the Row_count () function to judge, it is important to note that if the value before and after the update, Row_count is 0, Unlike the @ @rowcount in SQL Server or rowcount in Oracle, the number of rows affected will be greater than 0 if the update is to a row, regardless of whether the value of the field before and after the update has changed.

Example Description:
test on MySQL (database version: 5.1.30):
1. Create a database table:
CREATE TABLE T (
ID int,
name varchar,
address varchar (+),
primary KEY (Id,name)
) engine =innodb;

2. Insert the test data:
INSERT INTO T
(id,name,address)
Values
(1, ' Yubowei ', ' Weifang '),
(2, ' Sam ', ' Qingdao ');

3. Update the test
Update T
Set address = ' Weifang '
WHERE id = 1
and name = ' Yubowei ';
at this point, view the number of rows affected:
select Row_count (); ==〉 execution result is 0;

4. Test again
Update T
Set address = ' Beijing '
WHERE id = 1
and name = ' Yubowei ';

at this point, view the number of rows affected:
select Row_count (); ==〉 execution result is 1;
from the above test it can be concluded that only when the record is really modified in MySQL, Row_count will record the number of rows affected, otherwise if the record exists but no actual modification
the update is not logged to Row_count.

This is not the same as SQL rowcount in Oracle
tests on Oracle (Database version: 10G):
1. Create a database table:
CREATE TABLE T (
ID int,
name Varchar2 (),
address varchar2 (+),
primary KEY (Id,name)
);

2. Insert the test data:
INSERT INTO T
(id,name,address)
Values
(1, ' Yubowei ', ' Weifang '),
(2, ' Sam ', ' Qingdao ');

3. Update the test
Update T
Set address = ' Weifang '
WHERE id = 1
and name = ' Yubowei ';
at this point, view the number of rows affected:
V_rowcount: = sql%rowcount; ==〉 execution result is 1;

4. Test again
Update T
Set address = ' Beijing '
WHERE id = 1
and name = ' Yubowei ';
at this point, view the number of rows affected:
V_rowcount: = sql%rowcount; ==〉 execution result is 1;
from the above test it can be concluded that in Oracle, as long as the updated records exist, the number of rows affected will be accumulated regardless of the actual data being modified.

Note: So far there are no parameters that can be set for Row_count (). If necessary, it can only be achieved by other means.

Explanation and usage of found_row () and Row_count () in MySQL [Copy link]

Related Article

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.