Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select results from the middle of a sorted list?

Tags:

sql

mysql

limit

I've got a simple table with 300 rows, and after ordering them I want to select rows 11-50. Do I limit by 50 and remove the top 10 rows somehow?

like image 687
user398271 Avatar asked Jul 21 '10 17:07

user398271


2 Answers

SELECT * 
FROM table
ORDER BY somecolumn
LIMIT 10,40 

From MySQL's manual:
The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be nonnegative integer constants (except when using prepared statements).
With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1)

like image 188
grateful.dev Avatar answered Oct 03 '22 23:10

grateful.dev


The LIMIT syntax includes an offset value, so you'd use:

LIMIT 10, 40

...to get rows 11 - 50, because the initial offset row is zero (not 1).

like image 43
OMG Ponies Avatar answered Oct 03 '22 22:10

OMG Ponies