Miloš Đorđević
Miloš Đorđević

Reputation: 1

PostgREST API - mutiple OR conditions. Is it possible?

I'm trying to create a query using PostgREST API with multiple OR conditions.

Essentially I need PostgREST query similar to this SQL query

SELECT * FROM people
WHERE condition1 AND (condition2 OR condition3) AND (condition4 OR condition5)

I found multiple solution/examples for one OR condition. For example:

GET /people?grade=gte.90&or=(age.eq.14,and(age.gte.21,age.lte.27))

Above example translates to:

SELECT * FROM people WHERE grade>=90 AND (age=14 OR (age>=21 AND age <=27))

How to add more than one?

Any help is appreciated.

Upvotes: 0

Views: 21

Answers (1)

Laurence Isla
Laurence Isla

Reputation: 458

Every filter in a query parameter is separated with an AND by default when translating to SQL. For example:

GET /people?first_name=eq.Peter&last_name=eq.Smith&age=eq.30

Is equivalent in SQL to:

SELECT *
FROM people
WHERE first_name = 'Peter' AND last_name = 'Smith' AND age = 30

So, to get the query you want, you need to specify each or=(...) filter in their respective query parameter:

GET /people?<condition1>&or=(<condition2>,<condition3>)&or=(<condition4>,<condition5>)

As a side note, using and=(...) is more useful when you have to nest inside an or=(...) for example, since the query parameters won't work there.

Upvotes: 0

Related Questions