How do I get the nth highest value in a MySQL result set? Resolving cases using MySQL limit

Source: Internet
Author: User
one of the toughest problems in MySQL is how to get the first highest value in the result set n , such as querying for the second (or third n ) expensive product, and obviously not using a function like Max or min to query for it. However, we can use MySQL LIMIT to solve this problem.

    • first , the result set is sorted in descending order.

    • The second step is to use LIMIT the clause to obtain n the first expensive product.

The general query is as follows:

SELECT     column1, Column2,... From    TableORDER by Column1 Desclimit nth-1, Count;

Let's take a look at an example where products the structure of the table is as follows-

mysql> desc products;+--------------------+---------------+------+----- +---------+-------+| Field | Type | Null | Key | Default | Extra |+--------------------+---------------+------+-----+---------+-------+| ProductCode | varchar (15) | NO | PRI |       NULL | || ProductName | varchar (70) |     NO | |       NULL | || ProductLine | varchar (50) | NO | MUL |       NULL | || Productscale | varchar (10) |     NO | |       NULL | || ProductVendor | varchar (50) |     NO | |       NULL | || ProductDescription | Text |     NO | |       NULL | || Quantityinstock | smallint (6) |     NO | |       NULL | || Buyprice | Decimal (10,2) |     NO | |       NULL | || MSRP | Decimal (10,2) |     NO | |       NULL | |+--------------------+---------------+------+-----+---------+-------+9 rows in set 

View the row records in the following product tables:

Mysql> SELECT ProductCode, ProductName, Buypricefrom productsorder by Buyprice desc;+-------------+---------------- ----------------------+----------+| ProductCode | ProductName                          | buyprice |+-------------+--------------------------------------+----------+| s10_4962    | 1962 LANCIAA Delta 16V               | 103.42   | | s18_2238    | 1998 Chrysler Plymouth Prowler       | 101.51 |   | s10_1949    | 1952 Alpine Renault 1300             | 98.58 |    | s24_3856    | 1956 Porsche 356A Coupe              | 98.3     | | s12_1108    | 2001 Ferrari Enzo                    | 95.59    | | s12_1099    | 1968 Ford Mustang                    | 95.34    | ... +-------------+--------------------------------------+ ----------+110 rows in Set

Our mission is to find the product with the second highest price in the result set. You can use LIMIT clauses to select the second row, such as the following query (note: The offset 0 starts from, so you want to specify starting from 1 , then fetch a row of records):

SELECT ProductCode, ProductName, Buyprice from  productsorder by Buyprice Desclimit 1, 1;

Execute the above query statement to get the following results-

Mysql> SELECT ProductCode, ProductName, Buyprice from  productsorder by Buyprice Desclimit 1, 1;+-------------+--- -----------------------------+----------+| ProductCode | ProductName                    | buyprice |+-------------+--------------------------------+----------+| s18_2238    | 1998 Chrysler Plymouth Prowler | 101.51   |+-------------+--------------------------------+-------- --+1 row in Set

Similarly, get the third-highest and fourth-highest product information for: LIMIT 2, 1 and LIMIT 3, 1 .

Related articles:

How to get the nth record in each group of MySQL

MSSQL implementation MySQL limit SQL statement

Related videos:

limit Usage-boolean education swallow 18 MySQL Introductory video tutorial

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.