PostgreSQL - repeating rows from LIMIT OFFSET

前端 未结 2 781
清歌不尽
清歌不尽 2020-12-25 10:57

I noticed some repeating rows in a paginated recordset.

When I run this query:

SELECT \"students\".* 
FROM \"students\" 
ORDER BY \"students\".\"stat         


        
2条回答
  •  醉酒成梦
    2020-12-25 11:22

    Why does "foo" appear in both queries?

    Because all rows that are returned have the same value for the status column. In that case the database is free to return the rows in any order it wants.

    If you want a reproducable ordering you need to add a second column to your order by statement to make it consistent. E.g. the ID column:

    SELECT students.* 
    FROM students 
    ORDER BY students.status asc, 
             students.id asc
    

    If two rows have the same value for the status column, they will be sorted by the id.

提交回复
热议问题