COUNT与LIMIT在mysql查询 [英] COUNT with LIMIT in mysql query

查看:283
本文介绍了COUNT与LIMIT在mysql查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取总量的行使用LIMIT与我的查询,以避免两次查询。
是可能吗?

i need to get total amount of rows when using LIMIT with my query to avoid twice querying. is it possible?

推荐答案

使用 FOUND_ROWS()


对于具有LIMIT子句的SELECT,返回的行数没有LIMIT子句

For a SELECT with a LIMIT clause, the number of rows that would be returned were there no LIMIT clause

在SELECT查询之后,需要 CALC_FOUND_ROWS 关键字。示例从手册:

use the statement right after your SELECT query, which needs the CALC_FOUND_ROWS keyword. Example from the manual:

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

请注意,这会给数据库带来额外的压力,因为它必须找出完整的结果集每次。只有在需要时才使用 SQL_CALC_FOUND_ROWS

Note that this puts additional strain on the database, because it has to find out the size of the full result set every time. Use SQL_CALC_FOUND_ROWS only when you need it.

这篇关于COUNT与LIMIT在mysql查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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