【发布时间】:2019-07-18 18:12:34
【问题描述】:
我有一个示例 Ltree 结构,但我想将其作为 JSON 结构返回。 我试过搜索堆栈溢出,但结果给出了错误的响应。
create table node
(
id integer not null,
name varchar(255),
path ltree not null
);
我有这些数据
INSERT INTO node (id,name,path) VALUES (1,'Residential','1');
INSERT INTO node (id,name,path) VALUES (2,'Commercial','2');
INSERT INTO node (id,name,path) VALUES (3,'Industrial','3');
INSERT INTO node (id,name,path) VALUES (4,'Res type 1','1.4');
INSERT INTO node (id,name,path) VALUES (5,'Comm type 1','2.5');
INSERT INTO node (id,name,path) VALUES (6,'Industrial 1','3.6');
INSERT INTO node (id,name,path) VALUES (7,'Residential 2','1.4.7');
INSERT INTO node (id,name,path) VALUES (8,'Commercial 2','2.5.8');
INSERT INTO node (id,name,path) VALUES (9,'Industrial 2','3.6.9');
这就是我想通过查询收集的内容
[
{
"name": "Residentioal",
"children": [
{
"name": "Res type 1",
"children": [
{
"name": "Residential 2",
"children": []
}
]
}
]
},
{
"name": "Commercial",
"children": [
{
"name": "Comm type 1",
"children": [
{
"name": "Commercial 2",
"children": []
}
]
}
]
},
{
"name": "Industrial",
"children": [
{
"name": "Industrial 1",
"children": [
{
"name": "Industrial 2",
"children": []
}
]
}
]
}
]
我试过recursive with ..,但它一直在循环,没有返回正确的值。
【问题讨论】:
-
您的 ltree 路径不符合您的预期输出。 “商业”位于 id 1 和“工业 1”下。 “工业 2”位于不存在的 3.6 下。我在下面的示例中更正了数据。
-
@S-Man 对不起,我这边有错字
标签: sql postgresql psql ltree