PostgreSQL - 左连接数组的最后一项

时间:2015-04-27 21:04:51

标签: arrays postgresql

我在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包含一个数组,其中包含该组织的所有父层次结构,首先是根,最后一项是当前行的父级(如果存在)。

我需要执行查询以显示当前行信息,然后执行左连接以显示当前行的父名称。

我怎么能这样做呢?

有替代方法吗?

1 个答案:

答案 0 :(得分:1)

解决了以下问题:

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;