postgresql ltree中的多参数搜索
我计划实现一个使用ltree作为多级分类的数据库。然而,当我试图获取路径为x或y的条目时,我遇到了麻烦postgresql ltree中的多参数搜索,postgresql,ltree,Postgresql,Ltree,我计划实现一个使用ltree作为多级分类的数据库。然而,当我试图获取路径为x或y的条目时,我遇到了麻烦 new_table +-------+--------+---------+ | id | name | path | ---------------------------- | 1 | a | 001 | | 2 | b | 002 | | 3 | c | 001.001 | | 4
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
这是有效的:
从新的_表中选择id,其中路径符号|(or)在ltxtquery
中是可接受的,因此使用ltree@ltxtquery
操作符:
select id from new_table where path @ '001|002'; -- find labels 001 or 002
-- or
select id from new_table where path @ '001*|002*'; -- find labels starting with 001 or 002
尝试:
或者不适用于这两个选项存在问题,但是当存在“003.001”路径时,“003.001”仍将包含在查询结果中
select id from new_table where path ~ '001|002.*'