Paging in SQL with LIMIT/OFFSET sometimes results in duplicates on different pages

后端 未结 3 978
面向向阳花
面向向阳花 2021-02-13 10:31

I\'m developing an online gallery with voting and have a separate table for pictures and votes (for every vote I\'m storing the ID of the picture and the ID of the voter). The t

相关标签:
3条回答
  • 2021-02-13 11:17

    in my case this problem was due to the Null value in the Order By clause, i solved this by adding another Unique ID field in Order By Clause along with other field.

    0 讨论(0)
  • 2021-02-13 11:24

    The simples explanation is that you had some data added or some votes occured when you was looking at different pages.

    I am sure if you would sorte by ID or creation_date this issue would go away.

    I.e. there is no issue with your code

    0 讨论(0)
  • 2021-02-13 11:30

    Do you execute one query per page to display? If yes, I suspect that the database doesn't guarantee a consitent order for items with the same number of votes. So first query may return { item 1, item 2 } and a 2nd query may return { item 2, item 1} if both items have same number of votes. If the items are actually items 10 and 11, then the same item may appear on page 1 and then on page 2.

    I had such a problem once. If that's also your case, append an extra clause to the order by to ensure a consistent ordering of items with same vote number, e.g.:

    ORDER BY picture.vote, picture.ID

    0 讨论(0)
提交回复
热议问题