如何 select 来自 WHERE 条件的 JSON 对象的字段

How to select a field of a JSON object coming from the WHERE condition

我有这个table

id  name   json
1   alex   {"type": "user", "items": [ {"name": "banana", "color": "yellow"}, {"name": "apple", "color": "red"} ] }
2   peter  {"type": "user", "items": [ {"name": "watermelon", "color": "green"}, {"name": "pepper", "color": "red"} ] }
3   john   {"type": "user", "items": [ {"name": "tomato", "color": "red"} ] }
4   carl   {"type": "user", "items": [ {"name": "orange", "color": "orange"}, {"name": "nut", "color": "brown"} ] }

重要,每个 json 对象可以有不同数量的“项目”,但我需要的只是在 WHERE 条件中匹配的对象的“产品名称”。

我想要的输出是前两列和项目名称,其中颜色类似于 %red%:

id name  fruit
1  alex  apple
2  peter pepper
3  john  tomato
select id, name, ***** (this is what I don't know) FROM table
where JSON_EXTRACT(json, "$.items[*].color") like  '%red%'

我推荐json_table(),如果你是运行 MySQL 8.0:

select t.id, t.name, x.name as fruit
from mytable t
cross join json_table(
    t.js,
    '$.items[*]' columns (name varchar(50) path '$.name', color varchar(50) path '$.color')
) x
where x.color = 'red'

MariaDB 中未实现此功能。我们可以在数字 table:

的帮助下手动取消嵌套
select t.id, t.name, 
    json_unquote(json_extract(t.js, concat('$.items[', x.num, '].name'))) as fruit
from mytable t
inner join (select 0 as num union all select 1 union all select 2 ...) x(num)
    on x.num < json_length(t.js, '$.items')
where json_unquote(json_extract(t.js, concat('$.items[', x.num, '].color'))) = 'red'

您可以使用 JSON_EXTRACT() 函数和 Recursive Common Table Expression 来动态生成行,例如

WITH RECURSIVE cte AS 
(
  SELECT 1 AS n
  UNION ALL
  SELECT n + 1
    FROM cte
   WHERE cte.n < (SELECT MAX(JSON_LENGTH(json)) FROM t )
)
SELECT id, name, 
       JSON_UNQUOTE(JSON_EXTRACT(json,CONCAT('$.items[',n-1,'].name'))) AS fruit
  FROM cte
  JOIN t
 WHERE JSON_EXTRACT(json,CONCAT('$.items[',n-1,'].color')) = "red"

Demo