在postgres中对ltree的所有分支进行数学运算

时间:2015-08-18 10:03:58

标签: postgresql ltree

我有一棵这样的树:

               Country
                  |
                State
              ____|____
             /         \
          City_1       City_2
      ______|______
     /             \
Neighborhood_1     Neighborhood_2

在我的数据库中,我有这样的记录:

| path                               |
|------------------------------------|
| Country                            |
| Country.State                      |
| Country.State.City1                |
| Country.State.City2                |
| Country.State.City1.Neighborhood_1 |
| Country.State.City1.Neighborhood_2 |

我想从路径中搜索,并获取该路径的所有节点的所有记录,不包括匹配分支中的内容,如下所示:

| path                               |
|------------------------------------|
| Country                            |
| Country.State                      |
| Country.State.City1                |
| Country.State.City1.Neighborhood_1 |

1 个答案:

答案 0 :(得分:0)

使用运算符@>

select * from table where path @> Country.State.City1.Neighborhood_1

Official ltree Doc