我有JSON
列,其中包含JSON数组。我的方案是获取所有记录,其中url
的值是'"example.com/user1"'
存在。我无法为此操作编写查询。
Record1
[
{
"id": "1",
"firstname": "user1",
"url": "example.com/user1"
},
{
"id": "2",
"firstname": "user2",
"url": "example.com/user2"
}
]
Record2
[
{
"id": "1",
"firstname": "user3",
"url": "example.com/user3"
},
{
"id": "2",
"firstname": "user2",
"url": "example.com/user2"
}
]
......
......
......
Record10
[
{
"id": "1",
"firstname": "user10",
"url": "example.com/user10"
},
{
"id": "2",
"firstname": "user1",
"url": "example.com/user1"
}
]
我运行的查询是:
Select internal_id from users_dummy where JSON_EXTRACT(user_friends, '$[0].url') = "example.com/user1" or JSON_EXTRACT(user_friends, '$[1].url') = "example.com/user1";
因此,o / p为:
Record1, Record10
这是在记录中搜索值的正确方法吗?
提前致谢。
最佳答案
您可以这样使用JSON_SEARCH
:
SELECT *
FROM users_dummy
WHERE JSON_SEARCH(user_friends, 'one', 'example.com/user1', NULL, '$[*].url') IS NOT NULL
demo on dbfiddle.uk
如果使用的是对象而不是数组,则可以使用以下解决方案:
SELECT *
FROM users_dummy
WHERE JSON_SEARCH(user_friends, 'one', 'example.com/user1', NULL, '$.*.url') IS NOT NULL
demo on dbfiddle.uk
关于mysql - 在MySQL中检索JSON数组内的Json数据的值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59973329/