MySQL LIMIT

Summaryin this tutorial, you will learn how to use MySQL LIMIT clause to constrain the number of rows returned by the SELECT statement.

Introduction to MySQL LIMIT clause

The LIMIT clause is used in the SELECT statement to constrain the number of rows in a result set. The LIMIT clause accepts one or two arguments. The values of both arguments must be zero or positive integers.

The following illustrates the LIMIT clause syntax with two arguments:

SELECT 
    column1,column2,...
FROM
    table
LIMIT offset , count;

Let’s examine the LIMIT clause parameters:

  • The offset specifies the offset of the first row to return. The offset of the first row is 0, not 1.
  • The count specifies the maximum number of rows to return.

When you use LIMIT with one argument, this argument will be used to specifies the maximum number of rows to return from the beginning of the result set.

SELECT 
    column1,column2,...
FROM
    table
LIMIT count;

The query above is equivalent to the following query with the LIMIT clause that accepts two arguments:

SELECT 
    column1,column2,...
FROM
    table
LIMIT 0 , count;

Using MySQL LIMIT to get the first N rows

You can use the LIMIT clause to select the first N  rows in a table as follows:

SELECT 
    column1,column2,...
FROM
    table
LIMIT N;

For example, to select the first 10 customers, you use the following query:

复制代码
SELECT
 customernumber,
 customername,
 creditlimit
FROM
 customers
LIMIT 10;
复制代码

Using MySQL LIMIT to get the highest and lowest values

The LIMIT clause often used with ORDER BY clause. First, you use the ORDER BY  clause to sort the result set based on certain criteria, and then you use the LIMIT clause to find lowest or highest values.

See the following customers table in the sample database.

For example, to select 5 customers who have the highest credit limit, you use the following query:

复制代码
SELECT
 customernumber,
 customername,
 creditlimit
FROM
 customers
ORDER BY
 creditlimit DESC
LIMIT 5;
复制代码

And the following query returns 5 customers who have the lowest credit limit:

复制代码
SELECT
 customernumber,
 customername,
 creditlimit
FROM
 customers
ORDER BY
 creditlimit ASC
LIMIT 5;
复制代码

Using MySQL LIMIT to get the nth highest value

One of the toughest questions in MySQL is how to select the nth highest values in a result set e.g., select the second (or nth) most expensive product, which you cannot use MAX or MIN functions to answer. However, you can use MySQL LIMIT to answer those kinds of questions.

  1. First, you sort the result set in descending order.
  2. Second, you use the LIMIT clause to get the nth most expensive product.

The generic query is as follows:

复制代码
SELECT 
    column1, column2,...
FROM
    table
ORDER BY column1 DESC
LIMIT nth-1, count;
复制代码

Let’s take a look at an example.

See the following products result set:

复制代码
SELECT
 productName,
 buyprice
FROM
 products
ORDER BY
 buyprice DESC;
复制代码

Our task is to get the highlight product, which is the second most expensive product in the result set. In order to do so, you use LIMIT clause to select the second row as the following query: (notice that the offset starts from zero)

复制代码
SELECT
 productName,
 buyprice
FROM
 products
ORDER BY
 buyprice DESC
LIMIT 1, 1;
复制代码

In this tutorial, we have shown you how to use MySQL LIMIT clause to constrain the number of rows returned by the SELECT statement. We also introduced you to a technique to get the nth highest value.

原文:http://www.mysqltutorial.org/mysql-limit.aspx

标签: MySQL

«上一篇: CSS2中的伪类与伪元素
»下一篇: Async
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值