Mysql中limit的用法詳解

來源:互聯網
上載者:User

標籤:分頁   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的用法詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.