简体   繁体   English

json arrays 包含对象的查询

[英]Query of json arrays containing objects

It is stored in MySQL DB in the form below.它以下面的形式存储在 MySQL DB 中。

"config" is a JSON type and it's an array of objects. “config”是一个 JSON 类型,它是一个对象数组。

I want to query the data that has "config" containing the object "type" is "download".我想查询“config”包含object“类型”为“下载”的数据。

(ie data with "id=test2" and "id=test3" in the example below) (即以下示例中带有“id=test2”和“id=test3”的数据)

I'm having a lot of trouble doing queries that include JSON type.我在执行包含 JSON 类型的查询时遇到了很多麻烦。

"id":"test1", "config":[{"type": "get", "request": {}}]
"id":"test2", "config":[{"type": "set", "request": {}}, {"type": "download", "request": {}}]
"id":"test3", "config":[{"type": "download", "request": {}}, {"type": "reset", "request": {}}]

You can do this with json_extract() and json_contains() :您可以使用json_extract()json_contains()执行此操作:

select t.*
from mytable t
where json_contains(json_extract(config, '$[*].type'), '"download"')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM