在 MySQL 查询中使用 LIMIT 对结果进行分页

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11728704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 14:19:49  来源:igfitidea点击:

Use LIMIT to paginate results in MySQL query

mysqlpaginationlimit

提问by corsiKa

I want to fetch my results a 'page' at a time; I want the page number to be a parameter (in a JDBC prepared statement). Consider the following snippet

我想一次获取一个“页面”的结果;我希望页码是一个参数(在 JDBC 准备好的语句中)。考虑以下片段

SELECT * FROM thread t ORDER BY t.id LIMIT ((? - 1) * 20), 20

So ideally, this would result, for page 1, to LIMIT 0, 20.

因此,理想情况下,对于第 1 页,这将导致LIMIT 0, 20.

When I test

当我测试

SELECT * FROM thread t ORDER BY t.id LIMIT ((1 - 1) * 20), 20

I am told I have a syntax error. I don't see what it could be, though - it's just some simple math. All it tells me is

我被告知我有一个语法错误。不过,我不知道它可能是什么——这只是一些简单的数学运算。它告诉我的只是

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '((1 - 1) * 20), 20' at line 1

ERROR 1064 (42000):您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以获取在第 1 行的 '((1 - 1) * 20), 20' 附近使用的正确语法

What am I doing wrong with my LIMITclause, and how can I fix it?

我的LIMIT条款有什么问题,我该如何解决?

回答by Shawn

This cannot be done.

这是无法做到的。

See solution here: MySQL Math and COUNT(*) in LIMIT

在此处查看解决方案: MySQL Math 和 COUNT(*) in LIMIT

I would recommend using javascript or something to handle the first parameter (i.e. offset) such as: limit 0,20on first page and limit 21,20on second...

我建议使用 javascript 或其他东西来处理第一个参数(即偏移量),例如: limit 0,20在第一页和limit 21,20第二页上...

For example if your first page has a get variable in the url www.example.com?page=1

例如,如果您的第一页在 url www.example.com?page=1 中有一个 get 变量

offset = (page - 1)*20 ;
row_count = 20;
select * from table limit (offset, row_count);

回答by Prabodh Hend

Define Offset for the query using the following syntax

使用以下语法为查询定义偏移量

SELECT column FROM table 
LIMIT {someLimit} OFFSET {someOffset};

For example, to get page #1 (records 1-10), set the offset to 0 and the limit to 10;

例如,要获取第 1 页(记录 1-10),请将偏移量设置为 0,将限制设置为 10;

SELECT column FROM table 
LIMIT 10 OFFSET 0;

To get page #2 (records 11-20), set the offset to 10 where the limit to 10

要获取第 2 页(记录 11-20),请将偏移量设置为 10,其中限制为 10

SELECT column FROM table 
LIMIT 10 OFFSET 10;

回答by Matt

MySQL requires numeric constants for that LIMIT syntax.

MySQL 需要该 LIMIT 语法的数字常量。

From http://dev.mysql.com/doc/refman/5.7/en/select.html:

http://dev.mysql.com/doc/refman/5.7/en/select.html

The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be nonnegative integer constants, with these exceptions:

  • Within prepared statements, LIMIT parameters can be specified using ? placeholder markers.

  • Within stored programs, LIMIT parameters can be specified using integer-valued routine parameters or local variables.

LIMIT 子句可用于限制 SELECT 语句返回的行数。LIMIT 接受一个或两个数字参数,它们都必须是非负整数常量,但以下情况除外:

  • 在准备好的语句中,可以使用 ? 占位符。

  • 在存储的程序中,可以使用整数值的例程参数或局部变量来指定 LIMIT 参数。

Compute the constant on the Java side.

在 Java 端计算常量。