我在 Postgres 中有下表:
Column | Type | Modifiers
-------------------------+-----------------------------+------------------------------------------------
id | uuid | not null
name | character varying(255) | not null
parent_organization_ids | uuid[] | not null default '{}'::uuid[]
parent_organization_ids
列包含一个数组,其中包含该组织的所有父层次结构,首先是根,最后一项是当前行的父级(如果存在)。
我需要做一个查询来显示当前行的信息,然后做一个左连接来显示当前行的父名称。
我该怎么做左连接?
是否有其他方法可以做到这一点?
最佳答案
解决了以下问题:
SELECT org.name, parent.name
FROM organizations org
LEFT JOIN organizations parent on
org.parent_organization_ids[array_upper(org.parent_organization_ids, 1)]=parent.id;
关于arrays - PostgreSQL - 左连接数组的最后一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29905913/