MySQL 数据 - 实现分页的最佳方式?

MySQL Data - Best way to implement paging?(MySQL 数据 - 实现分页的最佳方式?)
本文介绍了MySQL 数据 - 实现分页的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我的 iPhone 应用程序连接到我的 PHP Web 服务以从 MySQL 数据库中检索数据.一个请求可以返回 500 个结果.

My iPhone app connects to my PHP web service to retrieve data from a MySQL database. A request can return 500 results.

实现分页和一次检索 20 个项目的最佳方法是什么?

What is the best way to implement paging and retrieve 20 items at a time?

假设我收到了数据库中的前 20 个广告.现在我该如何请求接下来的 20 个广告?

Let's say I receive the first 20 ads from my database. Now how can I request for the next 20 ads?

推荐答案

来自 MySQL 文档:

LIMIT 子句可用于限制 SELECT 语句返回的行数.LIMIT 接受一个或两个数字参数,它们都必须是非负整数常量(除非使用准备好的语句).

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 (except when using prepared statements).

有两个参数,第一个参数指定要返回的第一行的偏移量,第二个参数指定要返回的最大行数.初始行的偏移量为 0(不是 1):

With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1):

SELECT * FROM tbl LIMIT 5,10;  # Retrieve rows 6-15

要检索从某个偏移量到结果集末尾的所有行,您可以为第二个参数使用一些大数字.此语句检索从第 96 行到最后一行的所有行:

To retrieve all rows from a certain offset up to the end of the result set, you can use some large number for the second parameter. This statement retrieves all rows from the 96th row to the last:

SELECT * FROM tbl LIMIT 95,18446744073709551615;

带一个参数,该值指定从结果集的开头返回的行数:

With one argument, the value specifies the number of rows to return from the beginning of the result set:

SELECT * FROM tbl LIMIT 5;     # Retrieve first 5 rows

换句话说,LIMIT row_count 等价于 LIMIT 0, row_count.

In other words, LIMIT row_count is equivalent to LIMIT 0, row_count.

这篇关于MySQL 数据 - 实现分页的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致
What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
Why Mysql#39;s Group By and Oracle#39;s Group by behaviours are different(为什么 Mysql 的 Group By 和 Oracle 的 Group by 行为不同)
MySQL GROUP BY DateTime +/- 3 seconds(MySQL GROUP BY DateTime +/- 3 秒)