如何在 Mysql 8 中的 JSON 内的嵌套数组中搜索值?
How to search a value in a nested array inside a JSON in Mysql 8?
假设我有一个具有以下结构的 JSON 对象:
{
"_id":"0000abcdefg",
"type":"PP",
"subscription":{
"subscribers":{
"physicSubscribers":[
{
"civility":"M",
"lastname":"DOE",
"firstname":"John",
"emailAddress":"john-doe@something.com",
},
{
"civility":"M",
"lastname":"smith",
"firstname":"TED",
"emailAddress":"ted-smith@something.com",
}
]
}
}
}
如何通过subscription.subscribers.physicSubscribers[*].firsname 搜索文档,但在比较之前将值转换为小写?
我已经尝试了一些解决方案,但它总是返回一个空结果:
SELECT doc ->> '$'
FROM customers
WHERE lower(JSON_EXTRACT(doc,'$.subscription.subscribers.physicSubscribers[*].firstname')) = 'john'
谢谢!
在 WHERE 中使用 JSON_EXTRACT() 看起来不正确 - 它 returns 是您尝试与单个值进行比较的数组。所以你必须另外在这个数组中搜索。
有欧可以使用JSON_SEARCH,并指定正确的排序规则:
SELECT doc ->> '$'
FROM customers
WHERE JSON_SEARCH(doc,
'one',
'john' COLLATE utf8mb4_0900_ai_ci,
NULL,
'$.subscription.subscribers.physicSubscribers[*].firstname') IS NOT NULL
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=12d9f1c860d5433e26bbf9279c92f09c
假设我有一个具有以下结构的 JSON 对象:
{
"_id":"0000abcdefg",
"type":"PP",
"subscription":{
"subscribers":{
"physicSubscribers":[
{
"civility":"M",
"lastname":"DOE",
"firstname":"John",
"emailAddress":"john-doe@something.com",
},
{
"civility":"M",
"lastname":"smith",
"firstname":"TED",
"emailAddress":"ted-smith@something.com",
}
]
}
}
}
如何通过subscription.subscribers.physicSubscribers[*].firsname 搜索文档,但在比较之前将值转换为小写?
我已经尝试了一些解决方案,但它总是返回一个空结果:
SELECT doc ->> '$'
FROM customers
WHERE lower(JSON_EXTRACT(doc,'$.subscription.subscribers.physicSubscribers[*].firstname')) = 'john'
谢谢!
在 WHERE 中使用 JSON_EXTRACT() 看起来不正确 - 它 returns 是您尝试与单个值进行比较的数组。所以你必须另外在这个数组中搜索。
有欧可以使用JSON_SEARCH,并指定正确的排序规则:
SELECT doc ->> '$'
FROM customers
WHERE JSON_SEARCH(doc,
'one',
'john' COLLATE utf8mb4_0900_ai_ci,
NULL,
'$.subscription.subscribers.physicSubscribers[*].firstname') IS NOT NULL
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=12d9f1c860d5433e26bbf9279c92f09c