MySQL Return JSON array index based on property value

后端 未结 2 1857
忘掉有多难
忘掉有多难 2020-12-22 00:41

I have a table with JSON data like this:

{\"a\": [{\"color\": \"blue\", \"value\": 15}, {\"color\": \"red\", \"value\": 30}]}

I need to get

2条回答
  •  一生所求
    2020-12-22 01:40

    Use JSON_SEARCH() to find the path to blue.

    SELECT JSON_EXTRACT(my_data, REPLACE(JSON_SEARCH(my_data, 'one', 'blue'), '.color', '.value'))
    

    JSON_SEARCH will return a string like $.a[0].color. REPLACE changes that to $.a[0].value, then you extract that element.

提交回复
热议问题