使用 offset+limit 查找 mySQL 查询中的结果总数 [英] Find total number of results in mySQL query with offset+limit

查看:30
本文介绍了使用 offset+limit 查找 mySQL 查询中的结果总数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Codeigniter 进行分页功能,但我认为这通常适用于 PHP/mySQL 编码.

I'm doing a pagination feature using Codeigniter but I think this applies to PHP/mySQL coding in general.

我正在使用偏移量和限制检索目录列表,具体取决于每页需要多少结果.但是要知道所需的总页数,我需要知道(结果总数)/(限制).现在我正在考虑第二次运行 SQL 查询,然后计算所需的行数但不使用 LIMIT.但我认为这似乎是对计算资源的浪费.

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. But I think this seems to be a waste of computational resources.

有没有更好的方法?谢谢!

Are there any better ways? Thanks!

我的 SQL 查询也使用 WHERE 来选择具有特定category_id"的所有行

My SQL query uses WHERE as well to select all rows with a particular 'category_id'

推荐答案

看一看 SQL_CALC_FOUND_ROWS

这篇关于使用 offset+limit 查找 mySQL 查询中的结果总数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆