开发者

select next/previous 10 rows in 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 exactl开发者_运维百科y in order.

e.g:

 1,2,5,10,15 

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


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.


a possible query would be

SELECT * FROM mytable WHERE id > current_id LIMIT 10

for the 10 previous

SELECT * FROM mytable WHERE id < current_id ORDER BY id DESC LIMIT 10


First select 10 first value:

SELECT * FROM `leave_type` ORDER BY id asc limit 10;

and then

select * from `leave_type` limit 10, 10;

will show rows after 10th value(range of 10) and start with 11th.


(
SELECT  *
FROM    mytable
WHERE   id < $myid
ORDER BY
        id DESC
LIMIT 10
)
UNION ALL
(
SELECT  *
FROM    mytable
WHERE   id >= $myid
ORDER BY
        id
LIMIT 10
)
ORDER BY
        id


You can use limit keyword of MySQL.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜