問題描述
我的 iPhone 應(yīng)用程序連接到我的 PHP Web 服務(wù)以從 MySQL 數(shù)據(jù)庫中檢索數(shù)據(jù).一個(gè)請(qǐng)求可以返回 500 個(gè)結(jié)果.
My iPhone app connects to my PHP web service to retrieve data from a MySQL database. A request can return 500 results.
實(shí)現(xiàn)分頁和一次檢索 20 個(gè)項(xiàng)目的最佳方法是什么?
What is the best way to implement paging and retrieve 20 items at a time?
假設(shè)我收到了數(shù)據(jù)庫中的前 20 個(gè)廣告.現(xiàn)在我該如何請(qǐng)求接下來的 20 個(gè)廣告?
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 語句返回的行數(shù).LIMIT 接受一個(gè)或兩個(gè)數(shù)字參數(shù),它們都必須是非負(fù)整數(shù)常量(除非使用準(zhǔn)備好的語句).
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).
有兩個(gè)參數(shù),第一個(gè)參數(shù)指定要返回的第一行的偏移量,第二個(gè)參數(shù)指定要返回的最大行數(shù).初始行的偏移量為 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
要檢索從某個(gè)偏移量到結(jié)果集末尾的所有行,您可以為第二個(gè)參數(shù)使用一些大數(shù)字.此語句檢索從第 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;
帶一個(gè)參數(shù),該值指定從結(jié)果集的開頭返回的行數(shù):
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 等價(jià)于 LIMIT 0, row_count.
In other words, LIMIT row_count is equivalent to LIMIT 0, row_count.
這篇關(guān)于MySQL 數(shù)據(jù) - 實(shí)現(xiàn)分頁的最佳方式?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!