【问题标题】:Getting minimum of top n rows获得最少的前 n 行
【发布时间】:2015-11-12 07:15:55
【问题描述】:

我希望从 MySQL 的前 n 行中的表中获取 min(somecolumn)。获得结果的最佳查询是什么?

到目前为止我找到了

select min(a.column) from (select column from table limit 2000) a

select min(a.column) from table a INNER JOIN  (select column from table b limit 2000) on a.pricolumn = b.pricolumn.

【问题讨论】:

  • 你有什么问题?

标签: mysql limit rows minimum


【解决方案1】:

select min(t.columnName) from tableName as t limit 10(这里以前 10 行为例)

select outerTable.columnName from (select distinct t.columnName from tableName as t order by t.columnName asc limit 10) as outerTable limit 1,1

【讨论】:

  • 嗨 Prakit 感谢您的回复 stackoverflow.com/questions/4708708/mysql-order-by-limit 根据上述帖子查询“select min(t.columnName) from tableName as t limit 10”将从整个表中返回 min(t.columname) 而不是中间10 条记录,但您的第二个查询将起作用。但我担心的是第二个查询不使用索引,因此执行速度很慢
【解决方案2】:

试试下面的语法:

SELECT min(columname) FROM tablename limit n

【讨论】:

  • 嗨 Ritobroto,感谢您的回复 Query "select min(t.columnName) from tableName as t limit 10" 将从整个表中返回 min(t.columname) 而不是 10 条记录。
  • 请尝试:SELECT min(columname) FROM tablename WHERE columnname BETWEEN lowerlimit AND higherlimit LIMIT n;你想要这个吗?
猜你喜欢
  • 2016-07-30
  • 1970-01-01
  • 2020-06-13
  • 1970-01-01
  • 2017-05-04
  • 2019-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多