A small example of ROW_COUNT () in mysql

Source: Internet
Author: User

Note: in mysql, ROW_COUNT () can return the number of rows affected by the previous SQL UPDATE, DELETE, and INSERT operations.

MySQL test (database version: 5.1.22 ):

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:
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. Update 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 row_count records the number of affected rows only when the record is modified in MySQL, otherwise, if the record exists but is not modified, the update record will not be added to row_count.

Note:
After processing the UPDATE statement dynamically with PREPARE today, we found that the ROW_COUNT () function always returns-1. After checking that row_count () was originally placed behind the deallocate statement.
I made such a mistake.

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.