简体   繁体   English

使用 offset+limit 查找 mySQL 查询中的结果总数

[英]Find total number of results in mySQL query with offset+limit

I'm doing a pagination feature using Codeigniter but I think this applies to PHP/mySQL coding in general.我正在使用 Codeigniter 进行分页功能,但我认为这通常适用于 PHP/mySQL 编码。

I am retrieving directory listings using offset and limit depending on how many results I want per page.我正在使用偏移量和限制检索目录列表,具体取决于每页所需的结果数。 However to know the total number of pages required, I need to know (total number of results)/(limit).但是要知道所需的总页数,我需要知道(结果总数)/(限制)。 Right now I am thinking of running the SQL query a second time then count the number of rows required but without using LIMIT.现在我正在考虑第二次运行 SQL 查询,然后计算所需的行数,但不使用 LIMIT。 But I think this seems to be a waste of computational resources.但我认为这似乎是对计算资源的浪费。

Are there any better ways?有没有更好的方法? Thanks!谢谢!

EDIT: My SQL query uses WHERE as well to select all rows with a particular 'category_id'编辑:我的 SQL 查询也使用 WHERE 来选择具有特定“category_id”的所有行

SELECT COUNT(*) FROM table_name WHERE column = 'value' will return the total number of records in a table matching that condition very quickly. SELECT COUNT(*) FROM table_name WHERE column = 'value'将很快返回匹配该条件的表中的记录总数。

Database SELECT operations are usually "cheap" (resource-wise), so don't feel too bad about using them in a reasonable manner.数据库SELECT操作通常是“便宜的”(资源方面),所以不要对以合理的方式使用它们感到太糟糕。

EDIT: Added WHERE after the OP mentioned that they need that feature.编辑:在 OP 提到他们需要该功能之后添加了WHERE

Considering that SQL_CALC_FOUND_ROWS requires invoking FOUND_ROWS() afterwards, if you wanted to fetch the total count with the results returned from your limit without having to invoke a second SELECT , I would use JOIN results derived from a subquery :考虑到SQL_CALC_FOUND_ROWS需要在之后调用FOUND_ROWS() ,如果您想使用从限制返回的结果获取总计数而不必调用第二个SELECT ,我将使用从子查询派生的JOIN结果:

SELECT * FROM `table` JOIN (SELECT COUNT(*) FROM `table` WHERE `category_id` = 9) t2 WHERE `category_id` = 9 LIMIT 50

Note: Every derived table must have its own alias, so be sure to name the joined table.注意:每个派生表都必须有自己的别名,所以一定要命名连接表。 In my example I used t2 .在我的示例中,我使用了t2

The SQL_CALC_FOUND_ROWS query modifier and accompanying FOUND_ROWS() function are deprecated as of MySQL 8.0.17 and will be removed in a future MySQL version. SQL_CALC_FOUND_ROWS 查询修饰符和随附的 FOUND_ROWS() 函数自 MySQL 8.0.17 起已弃用,并将在未来的 MySQL 版本中删除。 As a replacement, considering executing your query with LIMIT, and then a second query with COUNT(*) and without LIMIT to determine whether there are additional rows.作为替代,考虑使用 LIMIT 执行查询,然后使用 COUNT(*) 且不使用 LIMIT 的第二个查询来确定是否还有其他行。 For example, instead of these queries:例如,而不是这些查询:

SELECT SQL_CALC_FOUND_ROWS * FROM tbl_name WHERE id > 100 LIMIT 10;
SELECT FOUND_ROWS();

Use these queries instead:请改用这些查询:

SELECT * FROM tbl_name WHERE id > 100 LIMIT 10;
SELECT COUNT(*) WHERE id > 100;

COUNT(*) is subject to certain optimizations. COUNT(*) 受某些优化的影响。 SQL_CALC_FOUND_ROWS causes some optimizations to be disabled. SQL_CALC_FOUND_ROWS 会导致一些优化被禁用。

you can use 2 queries as below, One to fetch the data with limit and other to get the no of total matched rows.您可以使用 2 个查询,如下所示,一个是获取有限制的数据,另一个是获取匹配行的总数。

Ex:前任:

SELECT * FROM tbl_name WHERE id > 1000 LIMIT 10;
SELECT COUNT(*) FROM tbl_name WHERE id > 1000;

As described by Mysql guide , this is the most optimized way, and also SQL_CALC_FOUND_ROWS query modifier and FOUND_ROWS() function are deprecated as of MySQL 8.0.17正如所描述的Mysql导向,这是最优化的方式,并且还SQL_CALC_FOUND_ROWS查询改性剂和FOUND_ROWS()函数被弃用的MySQL 8.0.17的

SELECT COUNT(id) FROM `table` WHERE `category_id` = 9

为您提供特定类别的行数。

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

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