如何使用JSON_SEARCH()在MySQL JSON数组中查找数字的路径? [英] How do I use JSON_SEARCH() to find the path of a number in a MySQL JSON array?
问题描述
注意:这是针对MySQL 5.7的。
Note: This is for MySQL 5.7.
当我尝试使用 JSON_SEARCH $ c $时c>在JSON数组中查找数字值的路径,我得到
NULL
。但是,当我使用 JSON_SEARCH
查找字符串时,实际上得到了路径。
When I attempt to use JSON_SEARCH
to find the path to a number value within a JSON array, I get NULL
. But when I use JSON_SEARCH
to look for a string, I actually get the path.
# String Search Example
SET @json = '[1, 2, 3, "abc"]';
SELECT JSON_SEARCH(@json, 'one', 'abc');
----------
| "$[3]" |
----------
...但是当我明确搜索一个数字值,我得到 NULL
?
...but when I explicitly search for a number value, I get NULL
?
# Number Search Example
SET @json = '[1, 2, 3, "abc"]';
SELECT JSON_SEARCH(@json, 'one', 1);
----------
| NULL |
----------
奇怪的是 JSON_CONTAINS
仍然可以按预期使用数字或字符串。
What's strange is that JSON_CONTAINS
still works as intended with numbers or strings.
这可能相似这个问题-> MYSQL触发器:JSON_SEARCH整数json数组中的整数值
This may be similar to this question -> MYSQL Triggers: JSON_SEARCH an integer value in a json array of integers
推荐答案
JSON_SEARCH 函数仅适用于文档中提到的字符串标量:函数搜索JSON值
JSON_SEARCH function works with only string scalars as mentioned in the documentation : Functions That Search JSON Values
在这里您可以看到:
已报告的错误- JSON_SEARCH不会搜索非字符串值
报告的功能-使JSON_SEARCH用于非字符串
这篇关于如何使用JSON_SEARCH()在MySQL JSON数组中查找数字的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!