MySQL数据-实现分页的最佳方法? [英] MySQL Data - Best way to implement paging?

查看:47
本文介绍了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?

推荐答案

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数据-实现分页的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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