[英]MySQL search in array of JSON objects
Lets say I have a JSON column json
with the following data:假设我有一个 JSON 列
json
具有以下数据:
+----+-----------------+
| id | json |
+====+=================+
| 1 | [ |
| | { |
| | "type":"a", |
| | "id":11111 |
| | }, |
| | { |
| | "type":"a", |
| | "id":22222 |
| | }, |
| | { |
| | "type":"b", |
| | "id":11111 |
| | } |
| | ] |
+----+-----------------+
| 2 | [ |
| | { |
| | "type":"b", |
| | "id":11111 |
| | |
| | }, |
| | { |
| | "type":"b", |
| | "id":22222 |
| | |
| | }, |
| | { |
| | "type":"c", |
| | "id":11111 |
| | } |
| | ] |
+----+-----------------+
Is there a way to determine if there is an entry in this array which has type = a
AND id = 11111
(in the same object), such that ROW 1 would be returned but not ROW 2?有没有办法确定此数组中是否存在
type = a
AND id = 11111
(在同一对象中)的条目,以便返回 ROW 1 但不返回 ROW 2?
What i've tried is something like我试过的是
SELECT *
FROM table
WHERE json->>"$[*].type" = "a"
AND json->>"$[*].id" = 11111
but that's not for the same entry.但这不是同一个条目。
Ofcourse I didn't stop looking - and found the solution after 3 hours of trail and error.当然,我并没有停止寻找——经过 3 小时的跟踪和错误,我找到了解决方案。 Feeling a little stupid now I know the solution.
现在我知道解决方案了,感觉有点愚蠢。
SELECT *
FROM table
WHERE JSON_CONTAINS(json, JSON_OBJECT('type', 'a', 'id', 11111));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.