postgresql - 在 postgresql ltree 中使用多个参数搜索

标签 postgresql ltree

我计划实现一个使用 ltree 作为多级分类的数据库。但是,当我尝试获取路径 x 或 y 的条目时遇到了麻烦。

         new_table
+-------+--------+---------+
|  id   |  name  |   path  |
----------------------------
|   1   |    a   |   001   |
|   2   |    b   |   002   |
|   3   |    c   | 001.001 |
|   4   |    d   | 002.001 |
|   5   |    e   |   003   |
----------------------------

根据下表,我想获得一个以 001 或 002 开头的 ID。但是我似乎无法获得正确的查询。

预期结果:1,2,3,4
这有效:select id from new_table where path <@ '001' or path <@ '002'
这不是(导致语法错误):select id from ingredient where ingredient_path <@ '001|002'

这让我感到困惑,因为 documentation声明使用 | (or) 符号是可以接受的。

我是 ltree 的新手,希望我能得到一个很容易理解的答案。

最佳答案

尝试:

select id from new_table where path ~ '001|002.*'

OR 不适用于 <@运营商我认为,根据文档,

ltree <@ ltree

|可用于 lquery相反

关于postgresql - 在 postgresql ltree 中使用多个参数搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44625459/

相关文章:

postgresql - 无法在postgresql中访问 super 用户

postgresql - 我们可以使用 PostgreSQL ltree 表示 DAG(有向无环图)吗?

postgresql - utf8 字符集中 Postgres Ltree 标签中的有效字符

sql - 使用像 ltree、json 这样的 postgres 数据类型是否与 NoSQL 相关?

python - 范围内每个数字的 SQL 计数总计(编辑 : via a UDF)

java - 根据列值不同的集合

sql - PostgreSQL:当 x=a 和 x=b 时如何从表中选择 2 个不同的计数

ruby-on-rails - 从 Rails 4.2 使用 UPSERT PostgreSQL

postgresql - 使用 Postgres ltree 收集所有叶节点