现象
数据查询的时候,第一页和第二页的数据不是完全相同,但是又一个重复数据
原因
去数据库查了,并没有重复数据
执行了sql
大概类似是这样的
select id,sex,name,scope
from student s
left join achieve a
on s.id=a.sid
where
name like '%王%'
group by s.id
order by
scope asc
limit 0,10
不分页的时候,数量是对得上的,limit 0,10 limit 10,10 limit 20,10的时候数量加一起也是对的,但是limit 0,10 和limit 10,10就有一个重复数据
然后发现查询的结果 order by的scope 这个字段值是一样的,然后查了资料发现,当order by 和limit同时出现的时候,执行顺序是form… where… select… order by… limit…
在完成 select 之后,所有记录是以堆排序的方法排列的,在进行 order by 时,仅把 scope 值大的往前移动,但是因为查询结果里scope 是一样的,堆排序又是无序的,所以sql limit拿的时候是看见什么拿什么,所以有概率第二页第一页数据重复
解决
加一个第二排序字段 id
select id,sex,name,scope
from student s
left join achieve a
on s.id=a.sid
where
name like '%王%'
group by s.id
order by
scope asc,id asc
limit 0,10