简体   繁体   English

MySQL - 如何在分页前计算行数?

[英]MySQL - How to count rows before pagination?

I am making a search page to find users. 我正在创建一个搜索页面来查找用户。 I have que query to find them and actually I can do the pagination with "LIMIT startRow, numberRows". 我有查询查找它们,实际上我可以用“LIMIT startRow,numberRows”进行分页。 But how could I count the total number of "registers" found before doing the pagination? 但是,如何计算在进行分页之前找到的“寄存器”总数? I would like to add at my search page, the number of the users found in a search. 我想在我的搜索页面添加搜索中找到的用户数。

I need something like: "Page 1 of 100". 我需要这样的东西:“第1页,共100页”。 I actually I have "Page 1" but I don't know how to count the total of results before paginate. 我实际上我有“第1页”,但我不知道如何计算分页前的结果总数。

¿Maybe could be necesary execute an extra query with "SELECT COUNT(*)"? ¿也许可能需要用“SELECT COUNT(*)”执行额外的查询? ¿Is there another way to count before pagination to avoid another query? ¿是否有另一种方法可以在分页前计算以避免另一个查询?

I use two sql queries, one for single word, and another for multiword: 我使用两个sql查询,一个用于单个字,另一个用于多字:

Base sql query (for single word and multi word search): 基本sql查询(用于单字和多字搜索):

"SELECT * FROM accounts AS A INNER JOIN profiles AS P ON A.account_id = P.account_id "

Single word condition: 单字条件:

"WHERE A.username LIKE ? OR P.name LIKE ? OR P.name LIKE ? OR P.surname LIKE ? OR P.surname LIKE ? LIMIT ?,?"

Multi word condition: 多字条件:

"WHERE CONCAT(P.name, ' ', P.surname) LIKE ? LIMIT ?,?"

Thanks a lot. 非常感谢。

Modify the query like this: 像这样修改查询:

SELECT SQL_CALC_FOUND_ROWS * FROM accounts ... LIMIT ...

This will return the same limited/offset result as before (the first page of results, for example), but if you then immediately send this second query to the server... 这将返回与之前相同的有限/偏移结果(例如,结果的第一页),但是如果您然后立即将此第二个查询发送到服务器...

SELECT FOUND_ROWS();

The server will return a count of the total number of rows that would have been returned if the LIMIT had not been imposed on the previous query. 如果尚未对先前的查询施加LIMIT ,则服务器将返回将返回的总行数。 There's your total number of results. 这是你的总结果数。

This will, of course, mean that your initial query takes longer, because the optimizer can't take any shortcuts that might allow it to stop evaluating rows once the LIMIT is satisfied, but in some cases, no such shortcuts are available anyway. 当然,这将意味着您的初始查询需要更长时间,因为优化器不能采用任何可能允许它在LIMIT满足后停止评估行的快捷方式,但在某些情况下,无论如何都没有这样的快捷方式。

This is the "official" mechanism for doing what you are trying to accomplish. 这是做你想要完成的事情的“官方”机制。

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

You could use do something like this: 你可以使用这样的事情:

SELECT (select count(*) from produtos) as counter, column1, column2, column3, column4 FROM accounts AS A INNER JOIN profiles AS P ON A.account_id = P.account_id WHERE A.username LIKE ? OR P.name LIKE ? OR P.name LIKE ? OR P.surname LIKE ? OR P.surname LIKE ? LIMIT ?,?;

Instead of selecting all (*), you use each column name, and create another column called counter. 您不必选择所有(*),而是使用每个列名称,并创建另一个名为counter的列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM