标签:

移植sql server 的存储过程到mysql中,遇到了sql server中的:

IF @@ROWCOUNT < 1

对应到mysql中可以使用 FOUND_ROWS() 函数来替换。

1. found_rows() 函数的文档:

http://dev.mysql.com/doc/refman/5.6/en/information-functions.html#function_found-rows

1)found_rows() 的第一种使用情况(带有SQL_CALC_FOUND_ROWS,也带有limit):

A SELECT statement may include a LIMIT clause to restrict the number of rows the server returns to the client. In some cases, it is desirable to know how many rows the statement would have returned without the LIMIT, but without running the statement again. To obtain this row count, include a SQL_CALC_FOUND_ROWS option in the SELECT statement, and then invoke FOUND_ROWS() afterward:

mysql> SELECT SQL_CALC_FOUND_ROWS * FROM tbl_name

-> WHERE id > 100 LIMIT 10;

mysql> SELECT FOUND_ROWS();

The second SELECT returns a number indicating how many rows the first SELECT would have returned had it been written without the LIMIT clause.

前面的带有limit的select语句如果加上了 SQL_CALC_FOUND_ROWS,那么接下来执行的 SELECT FOUND_ROWS(); 将返回前面语句不带limit语句返回的行数。

2)found_rows() 的第二种/第三中使用情况(不带有SQL_CALC_FOUND_ROWS):

In the absence of the SQL_CALC_FOUND_ROWS option in the most recent successful SELECT statement, FOUND_ROWS() returns the number of rows in the result set returned by that statement. If the statement includes a LIMIT clause, FOUND_ROWS() returns the number of rows up to the limit. For example, FOUND_ROWS() returns 10 or 60, respectively, if the statement includes LIMIT 10 or LIMIT 50, 10.

The row count available through FOUND_ROWS() is transient and not intended to be available past the statement following the SELECT SQL_CALC_FOUND_ROWS statement. If you need to refer to the value later, save it:

mysql> SELECT SQL_CALC_FOUND_ROWS * FROM ... ;

mysql> SET @rows = FOUND_ROWS();

If you are using SELECT SQL_CALC_FOUND_ROWS, MySQL must calculate how many rows are in the full result set. However, this is faster than running the query again without LIMIT, because the result set need not be sent to the client.

1>第二种使用情况(不带有SQL_CALC_FOUND_ROWS,也没有带 limit):

如果前面的select语句没有带 SQL_CALC_FOUND_ROWS,也没有带 limit ,那么后面的 SELECT FOUND_ROWS(); 返回的结果就是前面的select返回的行数;

2>第三中使用情况(不带有SQL_CALC_FOUND_ROWS,但是有带 limit):

如果前面的select语句没有带 SQL_CALC_FOUND_ROWS,但是带有 limit,那么后面的 SELECT FOUND_ROWS(); 返回的结果就是limit语句到达的最大的行数,比如:select * from xxx limit 10; 到达的最大的行数为10,所以 found_rows() 返回10;比如 select * from xxx limit 50,10; 它要从第50行开始,再扫描10行,所以到达的最大的行数为60,所以found_rows() 返回60。

这里第一个select found_rows() 返回105,因为他是从偏移100的地方,再扫描5行,所以返回105;但是第二个扫描的结果为空,select found_rows()返回了0!而不是105,因为 where userId=999999的结果为空,所以后面的 limit 100,5根本就没有执行。所以select found_rows()返回了0。

再看一个例子,更深入的理解其中情况下的 found_rows():

上面sql中 user_Pwd=xx 的值都是一样的。可以看到这种情况下的found_rows() 是对的select语句的中间结果,再 limit 时,此时的limit的扫描到的最大的行数。和原始表中的数据的行数,是没有关系的。他是对select的中间结果的limit,然后才得到最后的结果集,再返回。

3)SQL_CALC_FOUND_ROWS and FOUND_ROWS() 适合使用的场景

SQL_CALC_FOUND_ROWS and FOUND_ROWS() can be useful in situations when you want to restrict the number of rows that a query returns, but also determine the number of rows in the full result set without running the query again. An example is a Web script that presents a paged display containing links to the pages that show other sections of a search result. Using FOUND_ROWS() enables you to determine how many other pages are needed for the rest of the result.

1>SQL_CALC_FOUND_ROW + limit + found_rows() 可以使用在分页的场合。

2>不带SQL_CALC_FOUND_ROW 的 found_rows() 可以使用在存储过程中判断前面的select是否为空:

DELIMITER //

DROP PROCEDURE IF EXISTS loginandreg //

CREATE PROCEDUREloginandreg(

OUT userIdBIGINT,IN user_Pwd VARCHAR(32),IN user_MobileCode VARCHAR(16),IN user_RegIP VARCHAR(16)

)BEGIN

IF EXISTS(SELECT * FROM Users u WHERE u.user_MobileCode=user_MobileCode) THEN

SELECT u.userId INTO userId FROM Users u WHERE u.user_MobileCode=user_MobileCode AND u.user_Pwd=user_Pwd;IF FOUND_ROWS() < 1 THEN

SELECT -1 INTOuserId;END IF;ELSE

INSERT INTOUsers(user_Pwd,user_MobileCode,user_Visibility,user_Level,user_RegTime,user_RegIP,user_Collecter,user_Collected)VALUES (user_Pwd,user_MobileCode,6,6,NOW(),user_RegIP,0,0);SELECT LAST_INSERT_ID() INTOuserId;END IF;END //DELIMITER ;

上面存储过程中的:

SELECT u.userId INTO userId FROM Users u WHERE u.user_MobileCode=user_MobileCode AND u.user_Pwd=user_Pwd;IF FOUND_ROWS() < 1 THEN

SELECT -1 INTOuserId;END IF;

就是一个很好的使用的例子。

这种存储过程的场景中就可以使用 mysql 的 FOUND_ROWS() 替换 sql server 存储过程中的 IF @@ROWCOUNT < 1 语句。

标签:

mysql foundrows 并发_MySQL 中的 FOUND_ROWS() 函数相关推荐

  1. php mysql 时间戳查询_mysql中时间查询函数(包括时间戳)

    mysql中时间查询函数(包括时间戳) 这些函数都是MySQL自带的,可以直接使用在PHP写的MySQL查询语句中哦 1-CURDATE()或CURRENT_DATE()和CURTIME()或CURR ...

  2. mysql教程排序_MySQL中的排序函数field()实例详解

    前言 我们在日常开发过程中,排序是经常用到的,有时候有这样的需求. 比如,需要在查询结果中根据某个字段的具体值来排序.如下面例子 上面是一张个人信息 表,假如我们想按照'seiki','iris',' ...

  3. mysql中的found_MySQL 中的 FOUND_ROWS() 函数

    移植 IF @@ROWCOUNT < 1 对应到mysql中可以使用 FOUND_ROWS() 函数来替换. 1. found_rows() 函数的文档: 1) found_rows() 的第一 ...

  4. mysql提取数字_Mysql中实现提取字符串中的数字的自定义函数分享

    因需要在mysql的数据表中某一字符串中的字段提取出数字,在网上找了一通,终于找到了一个可用的mysql函数,可以有效的从字符串中提取出数字. 该mysql提取出字符串中的数字函数如下: 复制代码 代 ...

  5. mysql 过程 的函数的区别是什么意思_Mysql中存储过程和函数的区别是什么

    Mysql中存储过程和函数的区别是什么 发布时间:2020-12-03 10:01:49 来源:亿速云 阅读:105 作者:小新 这篇文章主要介绍了Mysql中存储过程和函数的区别是什么,具有一定借鉴 ...

  6. mysql iso 时间_mysql 中 时间和日期函数

    原文链接: mysql 中 时间和日期函数 - redfox - 博客园 http://www.cnblogs.com/redfox241/archive/2009/07/23/1529092.htm ...

  7. mysql空值判断函数_MySQL中的ifnull()函数判断空值

    我们知道,在不同的数据库引擎中,内置函数的实现.命名都是存在差异的,如果经常切换使用这几个数据库引擎的话,很容易会将这些函数弄混淆. 比如说判断空值的函数,在Oracle中是NVL()函数.NVL2( ...

  8. mysql中常用的sql函数_Mysql中的常用函数:

    Mysql中的常用函数: 1.字符串函数: (1).合并字符串 concat():// concat('M','y',"SQL",'5.5');== MySQL5.5//当传入的参 ...

  9. mysql 排序开窗函数_MySQL中实现开窗函数

    一.概述 row_number是数据库中常用的一个开窗函数,可以实现数据分区编号的功能,然而MySQL并不支持开窗函数.本文介绍了在MySQL中实现开窗函数的方法. 二.经典开窗函数 首先准备基础数据 ...

最新文章

  1. MethodTrace 生成的trace文件为空
  2. Windows CE 程序设计 (3rd 版)
  3. 关于JavaScript相关文章
  4. arcgis怎么做poi_跟着闫磊大神学ArcGIS,事半功倍
  5. Windows 10封装中出现“无法验证你的Windows安装”错误解决方法
  6. LetCode-MSSQL从不订购的客户
  7. 【codevs1262】不要把球传给我,非常无语的一道题目
  8. Windows操作系统下使用pip安装pygame
  9. OpenCV搜索文件夹中的图片并保存图片路径和信息
  10. 手把手教你如何扩展GridView之个性分页
  11. 原生js代码实现Ajax
  12. 列文伯格-马夸尔特拟合算法(Levenberg Marquardt Fitting)的C#实现
  13. window对象小结
  14. java非静态内部类如何创建对象实例
  15. canbus是什么意思_canbus.是什么意思
  16. Windows Server 2008 终端服务授权激活-离线
  17. Unable to find a single main class from the following candidates 问题解决
  18. 空中交通通信控制设备的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告
  19. class balanced loss pytorch 实现
  20. matlab roundn函数_columns函数的使用方法 matlab中round函数具体用法

热门文章

  1. Android测试——如何捕获Windows系统的屏幕输入
  2. 图像处理——在Python中使用OpenCV和PIL显示图像(legacy)
  3. Jupyter Notebook——如何快速地以当前文件夹目录打开 Jupyter Notebook
  4. Linux下压缩、解压缩、效率,linux tar bz、bz2、gz、zip
  5. ubuntu用户管理与权限操作实例
  6. LeetCode 70. 爬楼梯 (递归斐波那契 | 动态规划)
  7. 静态代码块 构造代码块 构造方法的执行顺序
  8. 敏捷开发日常跟进系列之三:故事板,看板
  9. Linux虚拟机-配置文件说明
  10. SpringBoot学习笔记(4):自定义的过滤器