Home > Database > Mysql Tutorial > body text

有关mysql中ROW_COUNT()的小例子

WBOY
Release: 2016-06-07 17:55:31
Original
924 people have browsed it

mysql中的ROW_COUNT()可以返回前一个SQL进行UPDATE,DELETE,INSERT操作所影响的行数

注:mysql中的ROW_COUNT()可以返回前一个SQL进行UPDATE,DELETE,INSERT操作所影响的行数。

MySQL上的测试(数据库版本为:5.1.22):

1.创建数据库表:
代码如下:
create table t(
id int,
name varchar(50),
address varchar(100),
primary key(id,name)
)engine =InnoDB;

2.插入测试数据:
代码如下:
insert into t
(id,name,address)
values
(1,'yubowei','weifang'),
(2,'sam','qingdao');

3.更新:
代码如下:
update t set address = 'weifang'
where id = 1 and name = 'yubowei';

此时查看影响的行数:
select row_count(); ==〉执行结果为0;

4.再更新:
代码如下:
update t set address = 'beijing'
where id = 1 and name = 'yubowei';

此时查看影响的行数:
select row_count(); ==〉执行结果为1;
从上面的测试可以得出在MySQL中只有真正对记录进行修改了的情况下,row_count才会去记录影响的行数,否则如果记录存在但是没有实际修改则不会将该次更新记录到row_count中。

备注:
今天用PREPARE动态处理了UPDATE语句后,发现ROW_COUNT()函数返回的老是-1 ,检查了下原来是把row_count()放到了deallocate 语句后面了。
神一般的我,犯了这样的错,哈。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!