Sasha
Sasha

Reputation: 8705

Mysql - JSON_CONTAINS find within array

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?

Upvotes: 0

Views: 1242

Answers (1)

ysth
ysth

Reputation: 98388

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

Upvotes: 2

Related Questions