小编Eri*_*low的帖子

连接后保留数组元素的顺序

我有一个返回 CTE 的查询,如下所示

+-----------+-------------+
|   node_id | ancestors   |
|-----------+-------------|
|         1 | []          |
|         2 | []          |
|         3 | [1]         |
|         4 | [2]         |
|         5 | [4, 2]      |
+-----------+-------------+
Run Code Online (Sandbox Code Playgroud)

我想要做的是加入表nodes并将该列中的 id 替换ancestors为表中的另一列nodes。这是我到目前为止的查询:

WITH RECURSIVE tree AS (
  -- snip --
)
SELECT node.entity_id AS id,
       array_remove(array_agg(parent_nodes.entity_id), NULL) AS ancestors
FROM tree
JOIN entity.nodes AS node ON node.id = tree.node_id
LEFT OUTER JOIN entity.nodes AS parent_nodes ON …
Run Code Online (Sandbox Code Playgroud)

postgresql cte array recursive

4
推荐指数
1
解决办法
2073
查看次数

标签 统计

array ×1

cte ×1

postgresql ×1

recursive ×1