標籤:分頁 limit mysql
對比Oracle、SQL Server,個人感覺MySql的分頁是最好用的,現在總結一下,寫在這裡。Mysql中limit的用法:
在我們使用查詢語句的時候,經常要返回前幾條或者中間某幾行資料,這個時候怎麼辦呢?不用擔心,mysql已經為我們提供了這樣一個功能。
SELECT * FROM table LIMIT [offset,] rows | rows OFFSET offset
LIMIT 子句可以被用於強制 SELECT 語句返回指定的記錄數。
LIMIT 接受一個或兩個數字參數。參數必須是一個整數常量。如果給定兩個參數,
第一個參數指定第一個返回記錄行的位移量,第二個參數指定返回記錄行的最大數目。初始記錄行的位移量是 0(而不是 1);
下面,我們來看一下官方的說明文檔:
https://dev.mysql.com/doc/refman/5.0/en/select.html
The LIMIT clause can be used when you would use TOP in Access or MS SQL.
譯: Limit關鍵字可以看做,你在Access或者MS SQL中用的TOP;
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).
譯:Limit關鍵字可以用來限制返回的行數的SELECT語句。限制有一個或兩個數字參數,必須兩個非負整數常量(使用預先處理語句時除外)。
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):
譯:傳兩個參數時,第一個參數指定返回第一行的位移量,而第二個參數指定的最大返回的行數。位移量最初的行是0(不是1):
SELECT * FROM tbl LIMIT 5,10; # Retrieve rows 6-15
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:
譯:從某一檢索所有行位移量結束的結果集,您可以使用一些值很大的第二個參數。這個語句檢索所有從第96行到最後一行:
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
In other words, LIMIT row_count is equivalent to LIMIT 0, row_count.
譯:換句話說,LIMIT row_count 相當於限制 LIMIT,row_count。
Mysql中limit的用法詳解