PostgreSQL 更改返回的行顺序 [英] PostgreSQL changing returned rows order

查看:104
本文介绍了PostgreSQL 更改返回的行顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为类别的表,其中包含 ID(long)、Name(varchar(50))、parentID(long) 和 shownByDefault(boolean) 列.

I have a table named categories, which contains ID(long), Name(varchar(50)), parentID(long), and shownByDefault(boolean) columns.

该表包含 554 条记录.所有showedByDefaultValues 都是'false'.
当我执行 'select id, name from Categories' 时,pg 返回我所有的类别,按 id 排序.
然后我更新表的一些行('update Categories set shownByDefault where parentId = 1'),更新OK.
然后,当我尝试执行返回所有类别的第一个查询时,它们是返回者的顺序非常奇怪.
添加order by"没有问题,但由于我使用 JPA 来获取此值,有人知道问题出在哪里,或者是否有办法解决此问题?

This table contains 554 records. All the shownByDefaultValues are 'false'.
When I execute 'select id, name from categories', pg returns me all the categories, orderer by its id.
Then I update some of the rows of the table('update categories set shownByDefault where parentId = 1'), update OK.
Then, when I try to execute the first query, which returns all the categories, they are returner with a very weird order.
I do not have problem to add 'order by', but since I am using JPA to get this values, anyone knows what the problem is or if there is a way to fix this?

推荐答案

那不是问题.SQL SELECT 返回的行的顺序是未定义的,除非它有 ORDER BY.您获取它们的顺序通常受它们在表中的存储顺序和/或语句使用的索引的影响.

That's not a problem. The order of rows returned by a SQL SELECT is undefined unless it has an ORDER BY. The order you get them is usually influenced by the order they are stored in the table and/or the indices that are used by the statement.

因此,在不使用 ORDER BY 的情况下依赖该顺序是一个非常非常糟糕的主意.

So depending on that order without using ORDER BY is a very, very bad idea.

如果您按某种顺序需要它们,只需指定即可.

If you need them in some order, simply specify that.

重要的是,表是一个一组的行而不是<一个 href="http://en.wikipedia.org/wiki/Sequence" rel="noreferrer">序列 行.

It is important that a table is a set of rows and not a sequence of rows.

这篇关于PostgreSQL 更改返回的行顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆