A2-02-06.DML-Using MySQL LIMIT to Constrain The Number of Rows Returned By SELECT Statement

转载自:http://www.mysqltutorial.org/mysql-limit.aspx

Using MySQL LIMIT to Constrain The Number of Rows Returned By SELECT Statement

 

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:

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.

mysql limit offset

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

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

 

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:

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

Try It Out

mysql limit first n rows

Using MySQL LIMIT to get the highest and lowest values

The LIMIT clause often used with the 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.

customers_table

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

Try It Out

mysql limit get highest values

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

Try It Out

mysql limit get lowest values

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:

Let’s take a look at an example. We will use the products table in the sample database for the demonstration.

products table

See the following products result set:

Try It Out

MySQL LIMIT products table

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)

Try It Out

mysql limit most second expensive product

In this tutorial, we have shown you how to use MySQL LIMIT clause to constrain the number of rows returned by the SELECT statement.

posted @ 2018-08-21 15:28  zhuntidaoren  阅读(163)  评论(0编辑  收藏  举报