Mysql中limit的用法详解

对比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。

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。