如何使用 SQL 从数据库列中选择前 10 个最大的数字?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4874731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 09:09:15  来源:igfitidea点击:

How can I select the top 10 largest numbers from a database column using SQL?

sql

提问by Hyman Roscoe

I have a database table which contains a column that records page hits for every entry.

我有一个数据库表,其中包含一个记录每个条目的页面点击次数的列。

I want to select the top 5 most hit pages from the database, but can't seem to find the right method to do so using just SQL. In particular I'm looking for one which doesn't involve selecting every entry and scanning through it afterwards using PHP.

我想从数据库中选择前 5 个最热门的页面,但似乎无法仅使用 SQL 找到正确的方法。特别是我正在寻找一个不涉及选择每个条目并随后使用 PHP 扫描的条目。

What's the best way to do this via SQL (if there is one)?

通过 SQL 执行此操作的最佳方法是什么(如果有)?

Thanks.

谢谢。

回答by eumiro

Try this approach:

试试这个方法:

SELECT column1, column2, hit_pages,...
FROM YourTable
ORDER BY hit_pages DESC
LIMIT 5

回答by diagonalbatman

In MySQL> SELECT * FROM table ORDER BY hits DESC limit 5;

在 MySQL> SELECT * FROM table ORDER BY hits DESC limit 5;

In Oralce> SELECT * FROM table ORDER BY hits DESC where rownum <5;

在奥拉尔塞> SELECT * FROM table ORDER BY hits DESC where rownum <5;

回答by Amar

SELECT TOP 10 price(column Name) from products(tablename) order by price desc;

SELECT TOP 10 price(column Name) from products(tablename) order by price desc;

Below is the output

下面是输出

price
263.5 
123.79 
97 
81 
62.5 
55 
53 
49.3 
46 
45.6 

回答by Ali Rasouli

for find 20th maximum value primary key with where condition:

使用 where 条件查找第 20 个最大值主键:

  declare @max20Pk int=0;
  select @maxPk=min(Pk_Transaction) from(
  select top 20  Pk_Transaction from tblTransaction with(nolock)
  where  Fk_ChargeType in(2,3)
  order by Pk_Transaction desc
  ) as dtt