记录mysql排序字段有重复值,分页数据错乱问题,下面2个sql 除了分页limit外,其他都一样,
但是第三页的结果却包含部分第二页的数据。
SELECT
id,
show_flag,
sort,
vote_title,
img_url,
max_option_count,
vote_option_type,
begin_time,
end_time,
join_limit,
create_time,
update_time,
create_user,
update_user,
is_deleted,
delete_time,
consume_flag,
consume_type,
consume_count,
content
FROM
vote
WHERE
( show_flag = 1 AND is_deleted = 0 )
GROUP BY
id
ORDER BY
begin_time DESC
LIMIT 20,
10
查了一下MySQL官网,对这种情况做了介绍:
https://dev.mysql.com/doc/refman/5.7/en/limit-optimization.html
“If multiple rows have identical values in the ORDER BY columns, the server is free to return those rows in any order, and may do so differently depending on the overall execution plan. In other words, the sort order of those rows is nondeterministic with respect to the nonordered columns.”
翻译:如果在ORDER BY列中有多个行具有相同的值,则服务器可以自由以任何顺序返回这些行,并且根据整体执行计划的不同,返回值可能会有所不同。 换句话说,这些行的排序顺序相对于无序列是不确定的。
解决方案:排序字段增加一个不可能重复的字段,如 order by integralnum ,id desc或begin_time_millisecond desc即可解决
标签:consume,mysql,字段,limit,time,错乱,排序,ORDER From: https://www.cnblogs.com/ghostmen/p/17616930.html