我正在尝试将Postgres中的函数转换为select我打算用作视图的查询.原因是我想通过select带有where子句的查询从客户端访问它,而不是像使用函数那样使用参数.该表表示树(和邻接列表),定义如下:
CREATE TABLE tree (
id serial primary key,
parent_id int references tree(id)
);
INSERT INTO tree (id, parent_id) VALUES
(1,null)
, (2,1), (3,2), (4,3), (5,3)
, (6,5), (7,6), (8,4), (9,8)
, (10,9), (11,9), (12,7)
, (13,12), (14,12), (15,11)
, (16,15), (17,16), (18,14)
, (19,13), (20,19), (21,20);
SELECT setval ('tree_id_seq', 21); -- reset sequence
-- This produces a tree like:
-- +-- <10>
-- /
-- +-- <4> -- <8> --- <9> -+- <11> …Run Code Online (Sandbox Code Playgroud)