我一直在使用 Postgresltree构造来存储层次结构。现在,我想收集树中的所有叶节点。有没有一个简单的机制来做到这一点?
CREATE TABLE foo
AS
SELECT node::ltree
FROM ( VALUES
('Top.Astronomy'),
('Top.Astronomy.Astrophysics'),
('Top.Pictures'),
('Top.Pictures.Stars')
) AS t(node);
Run Code Online (Sandbox Code Playgroud)
我如何返回
Top.Astronomy.Astrophysics
Top.Pictures.Stars
Run Code Online (Sandbox Code Playgroud)
@>一种方法是使用contains 运算符@>
SELECT *
FROM foo AS f1
WHERE NOT EXISTS (
SELECT *
FROM foo AS f2
WHERE f1.node @> f2.node
AND f1.node <> f2.node
);
node
----------------------------
Top.Astronomy.Astrophysics
Top.Pictures.Stars
(2 rows)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1834 次 |
| 最近记录: |