我有以下结构:
CREATE TABLE folder (
id integer primary key,
name varchar(255)
);
CREATE TABLE folder_path (
folder_id integer references folder(id),
path ltree
);
INSERT INTO folder (id,name) VALUES (1,'f1');
INSERT INTO folder (id,name) VALUES (2,'f2');
INSERT INTO folder_path (folder_id, path) VALUES (1,'1');
INSERT INTO folder_path (folder_id, path) VALUES (2,'1.2');
INSERT INTO folder_path (folder_id, path) VALUES (2,'2');
并且需要选择所有文件夹及其路径,如下所示:
[
{ id: 1,
name: 'f1',
paths: [
[{id: 1, name: 'f1'}]
]
}
{ id: 2,
name: 'f2',
paths: [
[{id: 1, name: 'f1'}, {id: 2, name: 'f2'}],
[{id: 2, name: 'f2'}]
]
]
我怎样才能做到这一点?谢谢。sqlfiddle:http ://sqlfiddle.com/#!15/6b974/12