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

查看:552
本文介绍了使用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.

我使用offset和limit取回目录列表关于我想要每页的结果。但是,要知道所需的总页数,我需要知道(总结果数)/(限制)。现在我想到第二次运行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天全站免登陆