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

后端 未结 3 984
面向向阳花
面向向阳花 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: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

提交回复
热议问题