记录mysql order by xxx limit xxx数据重复的问题

引用 http://vsalw.com/9768.html

记录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即可解决

posted @   ghostmen  阅读(55)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示