PostgreSQL:LIMIT + ORDER返回不一致的结果

时间:2013-10-10 19:28:16

标签: sql postgresql

每当我运行包含LIMITORDER BY关键字的查询时,我会得到不一致的结果,具体取决于LIMIT子句中指定的项目数:

例如,以下查询:

SELECT "people"."name" 
FROM "people" 
WHERE "people"."type" = 2 
ORDER BY score desc, number desc 
LIMIT 50

结果给了我这个:

[John, Jack, Amy, Brad, Tom, ...]

(John和Jack的scorenumber属性都有相同的值。事实上Amy,Brad和Tom也是如此。)

现在,当我跑:

SELECT "people"."name" 
FROM "people" 
WHERE "people"."type" = 2 
ORDER BY score desc, number desc 
LIMIT 5

我得到以下结果:

[Jack, John, Tom, Amy, Brad]

1 个答案:

答案 0 :(得分:4)

当按值的顺序相同时,数据库可以决定下一个排序是什么,并且它可能会在同一查询的多次运行中发生变化。

尝试按

添加二级订单
ORDER BY score, name