limit作用:将查询结果集的一部分取出来,通常使用在分页查询当中

  1. 分页的作用是为了提高用户的体验,因为一次全部都查出来,用户体验差,可以一页一页翻着看

limit怎么用?

完整用法:limit startIndex length
起始下标从0开始

缺省用法:limit 5;这是取前五

  1. #按照薪资降序,取出排名在前五名的员工
  2. select ename,sal from emp order by sal desc limit 5;#取前五
  3. select ename,sal from emp order by sal desc limit 0,5;

注意:MySQL当中limit在order by之后执行


#取出工资排名在【3-5】名的员工
select
    ename,sal
from
    emp
order by
    sal desc
limit
    2,3;// 2表示起始位置从下标2开始,就是第三条记录,3表示长度


#取出工资排名在【5 - 9】名的员工
select
    ename,sal
from
    emp
order by
    sal desc
limit
    4,5;