加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

PostgreSQL – 从LIMIT OFFSET重复行

发布时间:2020-12-13 16:27:34 所属栏目:百科 来源:网络整理
导读:我在分页的记录集中注意到一些重复的行. 当我运行这个查询: SELECT "students".* FROM "students" ORDER BY "students"."status" asc LIMIT 3 OFFSET 0 我得到: | id | name | status | | 1 | foo | active | | 12 | alice | active | | 4 | bob | active
我在分页的记录集中注意到一些重复的行.

当我运行这个查询:

SELECT "students".* 
FROM "students" 
ORDER BY "students"."status" asc 
LIMIT 3 OFFSET 0

我得到:

| id | name  | status |
    | 1  | foo   | active |
    | 12 | alice | active |
    | 4  | bob   | active |

下一个查询:

SELECT "students".* 
FROM "students" 
ORDER BY "students"."status" asc 
LIMIT 3 OFFSET 3

我得到:

| id | name  | status |
    | 1  | foo   | active |
    | 6  | cindy | active |
    | 2  | dylan | active |

为什么“foo”出现在这两个查询中?

Why does “foo” appear in both queries?

因为返回的所有行具有与状态列相同的值.在这种情况下,数据库可以按任意顺序返回行.

如果你想要一个可重复的排序,你需要添加一个第二列到你的order by语句使它一致.例如. ID列:

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

如果两行的状态列具有相同的值,则它们将按照id排序.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读