[英]How to regexp in MySql json string
Let's assume this users
table:让我们假设这个users
表:
-----------------------------------------
| id | ... | info |
-----------------------------------------
| 1 | ... | {"items":["132","136"]} |
I need to make a request to fetch users
that have items with id == 136
.我需要请求获取具有id == 136
项目的users
。
This following is the sql
I built but it does not work and I dont understand why:以下是我构建的sql
但它不起作用,我不明白为什么:
SELECT _u.id FROM users _u WHERE _u.info REGEXP '("items":)([)("136")(])'
Thank you in advance!先感谢您!
Here is one approach using the MySQL JSON functions:这是使用 MySQL JSON 函数的一种方法:
SELECT *
FROM yourTable
WHERE JSON_SEARCH(JSON_EXTRACT(json, "$.items"), 'one', "136") IS NOT NULL;
The call to JSON_EXTRACT
first extracts the JSON array under the items
key.对JSON_EXTRACT
的调用首先提取items
键下的 JSON 数组。 Then, we use JSON_SEARCH
to try to find an element "136"
.然后,我们使用JSON_SEARCH
来尝试查找元素"136"
。
Edit:编辑:
If you are certain that the JSON to be searched would always just be one key items
along with a single level JSON array, then REGEXP
might be viable here:如果您确定要搜索的 JSON 始终只是一个关键items
以及单个级别 JSON 数组,那么REGEXP
在这里可能是可行的:
SELECT *
FROM yourTable
WHERE json REGEXP '"items":\\[.*"136".*\\]';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.