Postgresql Postgres ltree多路径

Postgresql Postgres ltree多路径,postgresql,ltree,Postgresql,Ltree,好的,我有一个在名为path的列上有ltree的表。我想选择多个路径,但我不想有太多的OR语句。这是可能的还是最好的方法 路径: “学校,我的学校。*” “公司。关于。*” '测试。信息。内容。*' 查询: 从path~'schools.myschool.*'或path~'companys.about.*'或path~'testing.information.content.*您可以使用OR运算符将正则表达式合并为一个,并通过分离公共后缀: select 'schools.myschool.

好的,我有一个在名为path的列上有ltree的表。我想选择多个路径,但我不想有太多的OR语句。这是可能的还是最好的方法

路径:

  • “学校,我的学校。*”
  • “公司。关于。*”
  • '测试。信息。内容。*'
查询:


从path~'schools.myschool.*'或path~'companys.about.*'或path~'testing.information.content.*
您可以使用OR运算符将正则表达式合并为一个,并通过分离公共后缀:

select 'schools.myschool.*' ~ any(array[
    'schools.myschool.*', 'companies.about.*', 'testing.information.content.*'
]);
 ?column? 
----------
 t
SELECT content, path FROM threads 
WHERE path ~ '(schools.myschool|companies.about|testing.information.content).*'

我喜欢这个解决方案,但只是为了澄清一下,我认为OP的意思是作为通配符的
*
。例如,你的表达式应该与schools.myschool.yourclass匹配如果要将
作为分隔符,则应使用
\.
而不是
,如果后缀也应以点分隔,则应使用
\.*
\.+
表示。