簡體   English   中英

選擇第二,第三,第四,第五,最大數字

[英]Select second, third, fourth, fifth, largest number

我有一個關於如何在表中選擇第二,第三,第四和第五大數字的問題。 要選擇我使用的最大行:

$max = SELECT max(money) FROM table

現在,我要指定$second_max $third_max$fourth_max$fourth_max$fifth_max

有人知道如何更改我以前的SQL select max()輕松指定第二個max,第三個max等嗎?

我不想使用:

select money from table order by money desc limit 5;

因為我希望它們全部位於不同的變量中。

select money from table order by money desc LIMIT 5

可能最簡單的方法是將它們放在單獨的行上:

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

只是這個:

SELECT money
FROM yourtable
ORDER BY money DESC
LIMIT 5

假設您實際上在表中有5條以上的記錄,那么您將獲得5條記錄的結果集,並按最高貨幣值排序。

使用SQL

select money from table order by money desc limit 5;

五行分別是最大,次級,...貨幣價值。

在ORACLE中,您可以執行以下操作:

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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM