O
O
Oklick2017-01-12 18:40:35
PostgreSQL
Oklick, 2017-01-12 18:40:35

How to properly implement array iteration (jsonb) in Postgresql?

Good day.
Faced such a problem.
This code works (it doesn't find anything because params->'sex' is a number):

SELECT * FROM goods WHERE jsonb_exists_any(params->'sex', array['1', '2', '3', '4'])

And this one throws an error:
SELECT * FROM goods WHERE jsonb_exists_any(params->'sex', array[1, 2, 3, 4])

ERROR: function jsonb_exists_any(jsonb, integer[]) does not exist

The essence of the code is to iterate over the value of array[1, 2, 3, 4] and if it contains params->'sex', it is added to the selection.
params->'sex' stores a number.
Please tell me how to organize the code.
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Burov, 2017-01-12
@Oklick

(params->'sex')::text::integer = ANY (ARRAY[1,2,3]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question