1、普通的分页查询
SELECT … FROM … WHERE … ORDER BY … LIMIT n
返回的数据条数和 n相等
如:
select * from student where id = 1 order by register_time desc limit 3;
返回的是student表中的3条数据
2、指定偏移量,即limit m,n
如:
select * from student where id = 1 order by register_time desc limit 1,3;
返回的是数据是2,3,4行的数据,即从第二行开始,完后显示3条数据。
3、使用分页时,注意索引的使用,不然可能会引起性能问题。
如:
select * from student where id = 1 order by lesson_id desc limit 1,3;
这个语句中,在id,lesson_id上建联合索引是比较好的。因为随着数据量的增大,越往后,偏移量越大,跨越的数据就越多,相应的速度也就会变慢。
SELECT * FROM table LIMIT [offset,] rows | rows OFFSET offset
4、为了检索从某一个偏移量到记录集的结束所有的记录行,可以指定第二个参数为 -1
如:
select * from student where id = 1 order by register_time desc limit 1,-1;
返回的是从第二行开始,到整个表的最后一条数据,即出第一条之外的所有数据。
select * from student where id = 1 order by register_time desc limit 5;
相当于
select * from student where id = 1 order by register_time desc limit 0,5;