gpt4 book ai didi

sql - Postgresql 复制树表内的数据

转载 作者:行者123 更新时间:2023-11-29 11:24:50 25 4
gpt4 key购买 nike

我有一个树形结构的表,列是idcategoryparent_id

现在我需要将一个节点及其子节点复制到另一个节点,在复制时,类别必须相同,但具有新的 id 和 parent_id..

我的输入将是要复制的节点 & 要复制的目标节点

我已经在图像文件中解释了树结构..

我需要一个函数来这样做..,

PostgreSQL 版本 9.1.2

  Column   |  Type   |                    Modifiers                    
-----------+---------+-------------------------------------------------
id | integer | not null default nextval('t1_id_seq'::regclass)
category | text |
parent_id | integer |
Indexes:
"t1_pkey" PRIMARY KEY, btree (id)
Foreign-key constraints:
"fk_t1_1" FOREIGN KEY (parent_id) REFERENCES t1(id)
Referenced by:
TABLE "t1" CONSTRAINT "fk_t1_1" FOREIGN KEY (parent_id) REFERENCES t1(id)

最佳答案

(在 PostgreSQL 8.4.3 下测试)

以下查询为节点 4 下的子树分配新 ID(参见 nextval),然后找到相应的新 ID of parents(参见 >左连接).

WITH RECURSIVE CTE AS (
SELECT *, nextval('t1_id_seq') new_id FROM t1 WHERE id = 4
UNION ALL
SELECT t1.*, nextval('t1_id_seq') new_id FROM CTE JOIN t1 ON CTE.id = t1.parent_id
)
SELECT C1.new_id, C1.category, C2.new_id new_parent_id
FROM CTE C1 LEFT JOIN CTE C2 ON C1.parent_id = C2.id

结果(在您的测试数据上):

new_id  category    new_parent_id
------ -------- -------------
9 C4
10 C5 9
11 C6 9
12 C7 10

一旦你有了它,就很容易将它插入回表中,你只需要小心地将子树根与适当的父节点重新连接(在这种情况下为 8,请参见 COALESCE(new_parent_id, 8)):

INSERT INTO t1
SELECT new_id, category, COALESCE(new_parent_id, 8) FROM (
WITH RECURSIVE CTE AS (
SELECT *, nextval('t1_id_seq') new_id FROM t1 WHERE id = 4
UNION ALL
SELECT t1.*, nextval('t1_id_seq') new_id FROM CTE JOIN t1 ON CTE.id = t1.parent_id
)
SELECT C1.new_id, C1.category, C2.new_id new_parent_id
FROM CTE C1 LEFT JOIN CTE C2 ON C1.parent_id = C2.id
) Q1

之后,该表包含以下数据:

new_id  category    new_parent_id
------ -------- -------------
1 C1
2 C2 1
3 C3 1
4 C4 2
5 C5 4
6 C6 4
7 C7 5
8 C8 3
9 C4 8
10 C5 9
11 C6 9
12 C7 10

关于sql - Postgresql 复制树表内的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10025154/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com