当前位置:网站首页>Xiaobai learns MySQL - Statistical 'opportunism'
Xiaobai learns MySQL - Statistical 'opportunism'
2022-06-25 04:34:00 【bisal(Chen Liu)】
MySQL Chinese support LIMIT Clause , Limit the number of rows returned . If it's done first LIMIT, I also want to know whether to take LIMIT How many lines can be returned , But I don't want to execute the same statement again , What can I do now ?
here , You can use MySQL A function provided FOUND_ROWS() Characteristics to solve .
(1) First , Execute once to include SQL_CALC_FOUND_ROWS Option SELECT sentence ,
[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) perform FOUND_ROWS() function , The number of records in this table is returned ,
[email protected] 16:02: [test]> select found_rows();
+--------------+
| found_rows() |
+--------------+
| 15 |
+--------------+
1 row in set, 1 warning (0.00 sec) You can see , and COUNT(*) Number of records , It's the same ,
[email protected] 16:03: [test]> select count(*) from tt;
+----------+
| count(*) |
+----------+
| 15 |
+----------+
1 row in set (0.01 sec)The reason for the addition SQL_CALC_FOUND_ROWS, Because it means the number of results to be achieved , For later use FOUND_ROWS() Function to pave the way .
If you execute LIMIT I didn't bring it 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) perform FOUND_ROWS() The previous one is returned SELECT The actual number of rows returned by the statement ,
[email protected] 15:49: [test]> select found_rows();
+--------------+
| found_rows() |
+--------------+
| 1 |
+--------------+
1 row in set, 1 warning (0.00 sec)FOUND_ROWS() Data obtained , It's just temporary , Execute the next statement , It will fail. , If the logic needs to use , Can be saved in advance , For example, define variable storage in a program .
About FOUND_ROWS() There are relevant introductions in the official website documents ,
https://dev.mysql.com/doc/refman/5.7/en/information-functions.html

As for the principle , I haven't seen the code , But I think SQL_CALC_FOUND_ROWS It means that when executing , The number of records in the table may be counted once , Stored in a temporary variable or a temporary area , So we need to execute this immediately FOUND_ROWS(), Get this value , Although we should do what we should do , After all, it is equivalent to MySQL Did it instead of us , At least at the executive level , It's still helpful .
Xiaobai studies MySQL
《 Xiaobai studies MySQL - Incremental Statistics SQL The needs of 》
《 Xiaobai studies MySQL - You've encountered this kind of scenario where you can't log in ?》
《 Xiaobai studies MySQL - There are some differences between users created by different versions 》
《 Xiaobai studies MySQL - A tool for randomly inserting test data 》
《 Xiaobai studies MySQL - varchar Why are type fields often defined as 255?》
《 Xiaobai studies MySQL - A case of flexible index creation 》
《 Xiaobai studies MySQL - “ Be opportunistic ” The number of records in a statistical table 》
《 Xiaobai studies MySQL - Once slow SQL The positioning of 》
《 Xiaobai studies MySQL - Talk about the importance of data backup 》
《 Xiaobai studies MySQL - InnoDB Support optimize table?》
《 Xiaobai studies MySQL - table_open_cache The role of 》
《 Xiaobai studies MySQL - Table space defragmentation method 》
《 Xiaobai studies MySQL - Case sensitive problem solving 》
《 Xiaobai studies MySQL - only_full_group_by Validation rules for 》
《 Xiaobai studies MySQL - max_allowed_packet》
《 Xiaobai studies MySQL - mysqldump Parameter differences to ensure data consistency 》
《 Xiaobai studies MySQL - The query will lock the table ?》
《 Xiaobai studies MySQL - The problem of index key length limitation 》
《 Xiaobai studies MySQL - MySQL Will be affected by “ High water level ” Influence ?》
《 Xiaobai studies MySQL - Database software and initialization installation 》
《 Xiaobai studies MySQL - Chat 》
Recently updated articles :
《 Xiaobai studies MySQL - Incremental Statistics SQL The needs of 》
《 Several guesses about the design of Tencent conference number 》
《 China Super League and Guoan in the new season , Through the thorns 》
《Oracle Of CTAS Can I bring constraints and other attributes to the new table ?》
Recent hot articles :
《" Red Alert " Game open source code brings us a shock 》
Article classification and indexing :
边栏推荐
- Multithreading structure of gbase 8s
- Openmmlab environment configuration
- Office macro virus bounce shell experiment
- 什么是数据持久化?
- Thorough understanding of database transactions
- Laravel document sorting 1. Installation and Preliminary Configuration
- Detailed explanation of flex attributes in flex layout
- PHP encapsulates curl to send get and post request methods, and uses
- 记录小知识点
- CTF_ Web: advanced problem WP (5-8) of attack and defense world expert zone
猜你喜欢

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

Coinlist how to operate the middle lot number security tutorial

什么是存储引擎以及MySQL常见的三种数据库存储引擎

What is the storage engine and the three common database storage engines for MySQL

CTF_ Web: Advanced questions of attack and defense world expert zone WP (19-21)

论文阅读《LSD-SLAM: Large-Scale Direct Monocular SLAM》

微信小程序父子组件之间传值

OBS Browser+浏览器的基本使用

CTF_ Web:8-bit controllable character getshell

Gbase 8s index R tree
随机推荐
Laravel document sorting 9. Blade template
5 key indicators of SEO: ranking + traffic + session + length of stay + bounce rate
js中的concat()
[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.
Record small knowledge points
论文阅读《LSD-SLAM: Large-Scale Direct Monocular SLAM》
CTF_ Web: Advanced questions of attack and defense world expert zone WP (19-21)
Thorough understanding of database transactions
2021.6.14 notes
GBase 8s 锁的分类
Failed to install redis interface
Leetcode points to the leetcode road of offering II 091 house painting [dynamic planning] heroding
Gbase 8s stored procedure flow control
冰冰学习笔记:循环队列的实现
Comparison of towe/ JIRA / tapd / Zen collaboration platforms
A detailed summary of TCP connection triple handshake
mongodb集群
MySQL order by
Laravel document sorting 10. Request life cycle
讲座记录《多种空间大地测量技术的数据处理方法和应用》