0

I have this PostgreSQL table:

id | something
1  | ["something1", "something2", "something3"]
2  | ["something1"]
3  | ["something2", "something4"]

I am using this query to get all the datas having the string something1 in the something column:

select * from my_table where (something)::jsonb ? 'something1'

How can i modify (or also there's a better way) this query to get all the datas that contains something1 OR something2?

1 Answer 1

1

You can use ?:

where something::jsonb ? 'something1'

To check for several possible values, use ?| against a text array:

where something::jsonb ?| array['something1', 'something2']

This checks if any value from the array exists in the jsonb array. If you want to check if all array elements exist in the jsonb payload, then use ?& instead.

Sign up to request clarification or add additional context in comments.

2 Comments

This is exactly what I was looking for, thanks man!
@aletede91 can you tell how to write same query in mysql with same scenario ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.