当前位置:网站首页>小白学习MySQL - 统计的'投机取巧'
小白学习MySQL - 统计的'投机取巧'
2022-06-25 03:59:00 【bisal(Chen Liu)】
MySQL中支持LIMIT子句,限制返回行数。如果先执行了LIMIT,我又想知道不带LIMIT能返回多少行,但又不想再执行一次相同的语句,这时能怎么做?
此时,可以利用MySQL提供的一个函数FOUND_ROWS()特性来解决。
(1) 首先,执行一次包含SQL_CALC_FOUND_ROWS选项的SELECT语句,
[email protected] 15:49: [test]> select SQL_CALC_FOUND_ROWS * from tt limit 1;
+------+---------------------+--------+
| code | cdate | ctotal |
+------+---------------------+--------+
| AAA | 2022-01-01 00:00:00 | 1 |
+------+---------------------+--------+
1 row in set, 1 warning (0.00 sec)(2) 执行FOUND_ROWS()函数,返回的就是这张表的记录数,
[email protected] 16:02: [test]> select found_rows();
+--------------+
| found_rows() |
+--------------+
| 15 |
+--------------+
1 row in set, 1 warning (0.00 sec)可以看到,和COUNT(*)的记录数,是相同的,
[email protected] 16:03: [test]> select count(*) from tt;
+----------+
| count(*) |
+----------+
| 15 |
+----------+
1 row in set (0.01 sec)之所以加上SQL_CALC_FOUND_ROWS,因为他是表示需要取得的结果数,为后面使用FOUND_ROWS()函数做铺垫。
如果执行LIMIT时没带SQL_CALC_FOUND_ROWS,
[email protected] 15:49: [test]> select * from tt limit 1;
+------+---------------------+--------+
| code | cdate | ctotal |
+------+---------------------+--------+
| AAA | 2022-01-01 00:00:00 | 1 |
+------+---------------------+--------+
1 row in set (0.00 sec)执行FOUND_ROWS()返回的就是上一条SELECT语句实际返回的行数,
[email protected] 15:49: [test]> select found_rows();
+--------------+
| found_rows() |
+--------------+
| 1 |
+--------------+
1 row in set, 1 warning (0.00 sec)FOUND_ROWS()得到的数据,只是临时的,执行下一条语句,就会失效,如果逻辑中需要用到,可以提前保存,例如在程序中定义变量存储。
关于FOUND_ROWS()在官网文档中有相关的介绍,
https://dev.mysql.com/doc/refman/5.7/en/information-functions.html

至于原理上,我没看过代码,但是我觉得SQL_CALC_FOUND_ROWS意思就是在执行的时候,可能就统计了一次表的记录数,存到了临时变量或者某个临时区域,所以要紧接着执行这个FOUND_ROWS(),得到这个值,虽然事儿该做什么还做什么,毕竟相当于MySQL替代我们做了这件事儿,至少从执行层面,还是有些帮助的。
小白学习MySQL
《小白学习MySQL - varchar类型字段为什么经常定义成255?》
《小白学习MySQL - TIMESTAMP类型字段非空和默认值属性的影响》
《小白学习MySQL - InnoDB支持optimize table?》
《小白学习MySQL - table_open_cache的作用》
《小白学习MySQL - only_full_group_by的校验规则》
《小白学习MySQL - max_allowed_packet》
《小白学习MySQL - mysqldump保证数据一致性的参数差异》
《小白学习MySQL - MySQL会不会受到“高水位”的影响?》
近期更新的文章:
《充电宝的玄机》
近期的热文:
文章分类和索引:
边栏推荐
- Flutter Builder & FutureBuilder组件
- 智慧风电:数字孪生 3D 风机智能设备运维
- 2021.4.15 note the difference between let, const and VaR in ES6
- Laravel document sorting 7. View
- Doubts about judging the tinyint field type of MySQL
- 2. play the chromatic harmonica
- 【LeetCode】143. Rearrange linked list
- How to install opencv? Opencv download installation tutorial
- numpy np tips: numpy数组的squeeze等处理
- SQL, CTE, flg case problems
猜你喜欢

"Grammar sugar" -- my new programming knowledge

1. Phase II of the project - user registration and login

数字时代的“文艺复兴”?起底数字藏品,让人欢喜让人愁

Turn 2D photos into 3D models to see NVIDIA's new AI "magic"!

Flutter FittedBox组件
Zoran community

Finereport displays and hides column data according to conditions

讲座记录《多种空间大地测量技术的数据处理方法和应用》

Cesium graphic annotation circle, square, polygon, ellipse, etc

WMS仓储管理系统的使用价值,你知道多少
随机推荐
kenlm
Laravel document sorting 10. Request life cycle
Flutter FittedBox组件
95% 程序员都在这里摸鱼……
How many images can opencv open?
AI quantitative transaction (II) -- tushare financial data framework
List rendering in wechat applet
Upgrade cmake
Numpy NP tips: squeeze and other processing of numpy arrays
CMD operation MySQL in Windows
彻底理解数据库事务
SQL, CTE, FLG CASE问题
什么是持久化?redis 持久化中的RDB和AOF是什么?
Cesium 加载显示热力图
【LeetCode】143. Rearrange linked list
[openwrt] we recommend a domestically developed version of openwrt, an introduction to istoreos. It is very easy to use. It is mainly optimized. It solves the problem of Sinicization.
Introduction to intstream API
什么是存储引擎以及MySQL常见的三种数据库存储引擎
Laravel document sorting 1. Installation and Preliminary Configuration
5 key indicators of SEO: ranking + traffic + session + length of stay + bounce rate