简体   繁体   中英

Select second, third, fourth, fifth, largest number

I have a question about how to select the second, third, fourth, and fifth largest number in a table. To select the biggest row I use:

$max = SELECT max(money) FROM table

Right now I want to specify $second_max , $third_max , $fourth_max and $fifth_max .

Does someone know how to change my previous SQL select max() easy to specify second max, third max etc...?

I do not want to use:

select money from table order by money desc limit 5;

Because I want them all in different variables.

select money from table order by money desc LIMIT 5

Probably the easiest way is to get them on separate rows:

select t.money
from table t
group by t.money
order by money desc
limit 5;

The next easiest thing is to put them in a comma-separated list:

select group_concat(money order by money desc) as monies
from (select t.money
      from table t
      group by t.money
      order by money desc
      limit 5
     ) T

Just this:

SELECT money
FROM yourtable
ORDER BY money DESC
LIMIT 5

You'll get a 5-record result set, ordered by the top money values - assuming you actually have 5+ records in the table.

USE SQL

select money from table order by money desc limit 5;

The five rows are there as max, secondary,... value of money.

In ORACLE you could do the following :

SELECT *
FROM (
  SELECT ADRESSID, 
         ROW_NUMBER() OVER (ORDER BY ADRESSID DESC) AS ROW_NUM
  FROM ADRESSTABLE       
) t
WHERE ROW_NUM = 1
OR ROW_NUM = 3
OR ROW_NUM = 5;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM