当前位置:网站首页>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 :
边栏推荐
- What is persistence? What are RDB and AOF in redis persistence?
- A detailed summary of TCP connection triple handshake
- GBASE 8s活锁、死锁问题的解决
- A detailed summary of four handshakes (or four waves) over TCP connections
- SQL injection details
- Detailed explanation of flex attributes in flex layout
- 讲座记录《多种空间大地测量技术的数据处理方法和应用》
- A-table mouse over the display hand, the current line can be clicked
- 【esp32学习之路6——flash加密】
- @Requestbody solution get parameter is null
猜你喜欢

Unity Quad culls shaders with back faces and transparent parts

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

Gbase 8s overall architecture

无法安装redis接口

Coinlist queuing tutorial to improve the winning rate

Shutter fittedbox component

深度学习——几种学习类型

Office macro virus bounce shell experiment

5 key indicators of SEO: ranking + traffic + session + length of stay + bounce rate

CTF_ Web: Learn flask template injection (SSTI) from 0
随机推荐
@Requestbody solution get parameter is null
SQL, CTE, flg case problems
unity Quad剔除背面并剔除透明部分的shader
Laravel document sorting 8. Middleware
LabVIEW开发气体调节器
Gbase 8s stored procedure syntax structure
Lecture record: history and development of strapdown inertial navigation solution
什么是存储引擎以及MySQL常见的三种数据库存储引擎
UCLA | generative pre training for black box optimization
GBASE 8s的隔离级别介绍
Gbase 8s memory management
SQL注入详解
Acmstreamopen return value problem
CTF_ Web:8-bit controllable character getshell
Gbase 8s stored procedure execution and deletion
1. Phase II of the project - user registration and login
Comparison of towe/ JIRA / tapd / Zen collaboration platforms
GbASE 8s中的Blob 页(Blobspace page)
讲座记录《多种空间大地测量技术的数据处理方法和应用》
Should I use on or where for the left join