select id, code, type, classify_id, book_name,create_time, isbn_id, author, publisher, publication_date, view_expiry, price, discount, total_page, is_free_shipping, status, visits_num, buy_num, inventory, weight, support_chapter_buy, s_top, s_top_time, total_words, languages, edition, postage_flag, number
from book
WHERE type = '1' AND status = '1' AND del_flag = '0'
order by create_time desc LIMIT 30, 10;
这是之前写的分页查询图书的某个sql 按时间倒序返回结果,突然有天生产出问题了,上一页下一页老是出现相同的书籍,我看了下确实是这样的,最后发现是因为那批书籍的数据的create_time都是一样的导致MySQL没法排序了,最后加了id desc
才解决。
教训:当order by可能出现字段重复的时候得考虑下是不是增加一个不会重复的字段排序
官网描述:
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列中有相同的值,服务器可以自由地以任何顺序返回这些记录,并且可能会根据整个执行计划以不同的方式进行。换句话说,这些行的排序顺序对于无序的列来说是不确定的。