0

I have json column with following data:

[{"option_id": 1, "category_id": 1}, {"option_id": 2, "category_id": 2}]

I am trying to find records with option_id = 1

This is query I am trying:

select count(*) as aggregate from `complaint_forms` 
where json_contains(`outcome_options`, '1', '$."option_id"') 

Count is 0. What am I doing wrong here?

2
  • $.option_id requires option_id to be a key of the top-level object, not a nested object in an array. Commented Jun 21, 2021 at 21:09
  • Is there a way to search with this structure or I have to change it? Commented Jun 21, 2021 at 21:10

1 Answer 1

2

You want to specify the candidate as an object with the desired attribute and value, not just the value, and not specify a path:

select count(*) as aggregate from `complaint_forms` 
where json_contains(`outcome_options`, '{"option_id":1}')

fiddle

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

Comments

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.