-1

I wish to get min(somecolumn) from table from first n rows in MySQL. What is the best query to get the result?

So far I found

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.
Pang
  • 9,564
  • 146
  • 81
  • 122
codey
  • 11

2 Answers2

0

select min(t.columnName) from tableName as t limit 10 (Here n limit is for first 10 rows as example)

OR

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

Pratik Joshi
  • 11,485
  • 7
  • 41
  • 73
  • Hi Prakit thanks for your reply http://stackoverflow.com/questions/4708708/mysql-order-by-limit as per above post Query "select min(t.columnName) from tableName as t limit 10 " will return min(t.columname) from whole table not among 10 records but your second query will work. but my concern is second query not using index so it executes very slow – codey Aug 19 '15 at 10:33
0

Try the below syntax:

SELECT min(columname) FROM tablename limit n

  • Hi Ritobroto, thanks for your reply Query "select min(t.columnName) from tableName as t limit 10 " will return min(t.columname) from whole table not among 10 records. – codey Aug 19 '15 at 10:36
  • please try: `SELECT min(columname) FROM tablename WHERE columnname BETWEEN lowerlimit AND higherlimit LIMIT n;` Do you want this? – Ritobroto Mukherjee Aug 19 '15 at 10:43