mysql查询json数组中的某个值

mysql5.7版本后支持json字段的检索查询。

  • 使用 字段->'$.json属性' 进行查询条件
  • 使用 json_extract 函数查询 ,json_extract(字段,"$.json属性")
  • 根据 json数组 查询,JSON_CONTAINS(字段,JSON_OBJECT('json属性', "内容"))

1、使用 字段->'$.json属性'进行查询条件

{"name":"张三","age":"15"}

指定查询str字段中age值为15的数据:

select * from json_test where str->'$.age' = '15'

 

2、使用json_extract函数查询,json_extract(字段,"$.json属性")

查询条件同上:

select * from json_test where json_extract(str,"$.age")

3、根据json数组查询,用JSON_CONTAINS(字段,JSON_OBJECT('json属性', "内容"))

例如字段str存储的JSON数组为:

[{"name":"李四","age":"16"},{"name":"王五","age":"17"}]

查询条件为查询数组中name为李四的数据:

select * from json_test where JSON_CONTAINS(str,JSON_OBJECT('name', "李四"))

 

posted @ 2022-02-15 16:47  凉年技术  阅读(13090)  评论(0编辑  收藏  举报