我正在为 WordPress 使用运行相同的源代码和相同的数据库,只有 mysql 版本不同。它使我的分页帖子重复。这是我发现的。知道如何在更高版本中解决它吗?
这是针对第 2 页和第 3 页的查询输出。左侧是 MySql 5.5,右侧是 MySql 5.6。up_posts.ID 是唯一的。
SQL语句:-
SELECT SQL_CALC_FOUND_ROWS up_posts.ID FROM up_posts WHERE 1=1 AND up_posts.post_type = 'post' AND (up_posts.post_status = 'publish' OR up_posts.post_status = 'closed' OR up_posts.post_status = 'private' OR up_posts.post_status = 'hidden') ORDER BY up_posts.post_date DESC LIMIT 10, 10
SELECT SQL_CALC_FOUND_ROWS up_posts.ID FROM up_posts WHERE 1=1 AND up_posts.post_type = 'post' AND (up_posts.post_status = 'publish' OR up_posts.post_status = 'closed' OR up_posts.post_status = 'private' OR up_posts.post_status = 'hidden') ORDER BY up_posts.post_date DESC LIMIT 20, 10
好像MySQL 5.5会按照ID排序,但是5.6以上没有这个能力。
ORDER BY up_posts.post_date DESC, up_posts.ID DESC
不再添加重复结果。似乎我们需要手动添加它以避免不必要的问题。供参考。它适用于 MySQL 5.6 和 5.7。
参见8.2.1.16 LIMIT 查询优化
如果您想按主键排序,则必须明确说明。