当前位置:网站首页>小白学习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会不会受到“高水位”的影响?》
近期更新的文章:
《充电宝的玄机》
近期的热文:
文章分类和索引:
边栏推荐
- Laravel document sorting 10. Request life cycle
- 无法安装redis接口
- Laravel document sorting 9. Blade template
- 单元测试覆盖率
- Turn 2D photos into 3D models to see NVIDIA's new AI "magic"!
- UCLA | 用于黑盒优化的生成式预训练
- Communication problems in parent and child components of uniapp
- Sourcetree pulls the code and prompts to fill in authentic, but the configuration cannot change the user
- 1280_ C language to find the average value of two unsigned integer
- Upgrade cmake
猜你喜欢

Cesium 加载显示热力图

Text keyword extraction: ansj

智慧风电:数字孪生 3D 风机智能设备运维

单元测试覆盖率

"How to carry out industrial positioning" in local / Park industrial planning

numpy np tips:使用opencv对数组插值放缩到固定形状 cv2.resize(res, dsize=(64, 64), interpolation=cv2.INTER_CUBIC)

Lecture record: new application of inertial navigation - inertial measurement

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

《Missing Parts》NFT 作品集第 5 系列上线 The Sandbox 市场平台

1. Phase II of the project - user registration and login
随机推荐
Hello CTP (I) - basic knowledge of futures
Simple integration of client go gin -update
EasyRecovery15非常好用的电脑数据恢复软件
《Missing Parts》NFT 作品集第 5 系列上线 The Sandbox 市场平台
Flutter Builder & futurebuilder components
NFT insider 63: the sandbox reached a cooperation with Time magazine, and YGG established Spain's subdao
LeetCode 劍指Offer II 091 粉刷房子[動態規劃] HERODING的LeetCode之路
Is opencv open source?
Vigilance against over range collection of privacy - ten mobile app violations
Cesium graphic annotation circle, square, polygon, ellipse, etc
PHP code audit 1 - php Ini
What is the difference between learning code, rolling code and fixed code? The number of repeated codes, coding capacity and the principle of rolling code
Introduction to intstream API
PostgreSQL数据库WAL——RM_HEAP_ID日志记录动作
Standing wave ratio calculation method
Intel 13th generation core showed its true colors for the first time: 68mb cache improved significantly
L'épée leetcode fait référence au chemin leetcode de l'offre II 091 pour peindre la maison [planification dynamique] heroding
关于TCP连接三次握手的详细总结
SQL, CTE, FLG CASE问题
微信小程序父子组件之间传值