5

I've got a postgres column 'data' which is jsonb in this format:

{
  'tags': ['friend','enemy','frenemy']
  ... // other data
}

Say I want to select all rows that are tagged either 'enemy' or 'frenemy', how can i do that?

I know I can select on a single tag with

SELECT * FROM people WHERE people.data->'tags' ? 'enemy'

I should be using @> ANY, but can't quite figure out the syntax for the to make it work.

7

I found the answer tucked away in this SO question.

SELECT * FROM people WHERE people.data->'tags' ?| ARRAY['enemy','frenemy']

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.