Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I select rows in MySQL starting at a given row number?

Say I have 50 rows in a MySQL table. I want to select the first ten (LIMIT 10), but then I want to be able to select the next 10 on a different page.

So how do I start my selection, after row 10?

Updated query:

mysql_query("     SELECT * FROM `picdb`     WHERE `username` = '$username'     ORDER BY `picid` DESC     LIMIT '$start','$count' ") 
like image 708
mrpatg Avatar asked Aug 10 '09 00:08

mrpatg


People also ask

How do I select a specific range of a row in SQL?

MySQL select rows by range using ROW_NUMBER() function The ROW_NUMBER() is a window function in MySQL that assigns a sequential number to each row. Hence we can use the row_number() function to select rows in a particular range.


2 Answers

I recommend working by obtaining the first page using:

LIMIT 0, 10 

then for the second page

LIMIT 10, 10 

then

LIMIT 20, 10 

for the third page, and so on.

like image 145
chaos Avatar answered Sep 25 '22 11:09

chaos


LIMIT 10  LIMIT 10 OFFSET 10 

From the MySQL 5.1 docs on SELECT syntax:

For compatibility with PostgreSQL, MySQL also supports the LIMIT row_count OFFSET offset syntax.

like image 37
Karl Voigtland Avatar answered Sep 22 '22 11:09

Karl Voigtland