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

时间:2011-02-02 12:57:47

标签: sql

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

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

通过SQL(如果有的话)最好的方法是什么?

感谢。

4 个答案:

答案 0 :(得分:5)

尝试这种方法:

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

答案 1 :(得分:3)

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

在Oralce&gt; SELECT * FROM table ORDER BY hits DESC where rownum <5;

答案 2 :(得分:1)

从产品(tablename)订购SELECT TOP 10价格(列名称)按价格desc;

以下是输出

price
263.5 
123.79 
97 
81 
62.5 
55 
53 
49.3 
46 
45.6 

答案 3 :(得分:0)

查找条件条件为第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