Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select next/previous 10 rows in mysql

Tags:

mysql

I have a list that displays only 10 rows.

I need to select the next 10 and previous 10 rows.

However, the ID I use as reference are not exactly in order.

e.g:

 1,2,5,10,15 

How do I select the next few rows using the ID?

like image 854
tep Avatar asked Apr 29 '11 10:04

tep


People also ask

How do I select the last 10 rows in SQL?

Let us now implement the above query. mysql> SELECT * FROM ( -> SELECT * FROM Last10RecordsDemo ORDER BY id DESC LIMIT 10 -> )Var1 -> -> ORDER BY id ASC; The following is the output that displays the last 10 records. We can match both records with the help of the SELECT statement.

How do I select the first 10 rows in MySQL?

To select first 10 elements from a database using SQL ORDER BY clause with LIMIT 10. Insert some records in the table using insert command. Display all records from the table using select statement. Here is the alternate query to select first 10 elements.

How do I get bottom 10 records in SQL?

Using DESC will return the last N rows, but the returned rows will also be in reverse order from the first N rows.

How do I select the last 3 rows in SQL?

I want to select the last 3 rows of an sql table. I know I should use SELECT * FROM table ORDER BY DESC LIMIT 3 , but the problem with this code is that it selects the rows from the end. For example, it selects 30, then 29, then 28.


1 Answers

you can try limit:

select * from `table` limit <startIndex>,<NumberOfRecords>;

example:-

select * from `user` limit 5,10;

This will return 10 rows starting from 6th row.

like image 142
Harry Joy Avatar answered Oct 15 '22 08:10

Harry Joy