Silent Knight
Silent Knight

Reputation: 21

Postgres ltree multiple paths

Okay, so I have a table with ltree on a column called path. I want to select multiple paths, but I don't want to have a ton of OR statements. Is that possible or is this the best way to do it?

Paths:

Query:

SELECT content, path FROM threads WHERE path ~ 'schools.myschool.*' OR path ~ 'companies.about.*' OR path ~ 'testing.information.content.*

Upvotes: 2

Views: 2347

Answers (2)

clemens
clemens

Reputation: 17712

You can join the regular expressions into one with the or-operator |, and by separating the common suffix:

SELECT content, path FROM threads 
WHERE path ~ '(schools.myschool|companies.about|testing.information.content).*'

Upvotes: 1

Clodoaldo Neto
Clodoaldo Neto

Reputation: 125454

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

Upvotes: 2

Related Questions