gpt4 book ai didi

mysql - JSON_Search() 返回 null

转载 作者:行者123 更新时间:2023-11-30 21:25:30 30 4
gpt4 key购买 nike

我有一个查询问题。在我的数据库中,我有一个包含以下格式的 JSON 的字段:

[[0, 16, 22, 37, 0, 0, 0, 71, 82], 
[0, 18, 0, 36, 43, 0, 60, 0, 88],
[9, 10, 0, 0, 0, 58, 69, 77, 0]]

用这个查询

SELECT JSON_SEARCH(NumeriJSON, 'all', 77,null, '$[*]') AS Indice FROM Cartella WHERE JSON_CONTAINS(NumeriJSON->'$[*]', '77')

我想获取数字在 JSON 中的位置,但它返回 null。为什么? JSON 的结构是有效的,因为 JSON_CONTAINS 工作得很好。非常感谢。

最佳答案

https://dev.mysql.com/doc/refman/8.0/en/json-search-functions.html#function_json-search说:

  • JSON_SEARCH(json_doc, one_or_all, search_str[, escape_char[, path] ...])

    Returns the path to the given string within a JSON document.

我给string这个词加了底气。如果 JSON 中的值为整数,则 JSON_SEARCH() 不起作用。

如果您将它们设为字符串,则您的示例有效:

insert into Cartella (NumeriJson) values (
'[["0", "16", "22", "37", "0", "0", "0", "71", "82"],
["0", "18", "0", "36", "43", "0", "60", "0", "88"],
["9", "10", "0", "0", "0", "58", "69", "77", "0"]]');

mysql> select json_search(numerijson, 'all', '77', null, '$[*]') as `index`
from cartella where json_contains(numerijson->'$[*]', '"77"');
+-----------+
| index |
+-----------+
| "$[2][7]" |
+-----------+

这个问题是 reported as a bug in 2015 , 但它通过简单地记录它只支持字符串搜索而被关闭。

当时支持非字符串是 reported as a new feature request in 2018 .到目前为止,还没有解决方案。

同时,如果要使用 JSON_SEARCH(),则必须将整数存储为字符串。

我还建议停止使用 JSON,而是将整数存储在普通的行和列中。然后搜索整数就可以正常工作。

附言请不要使用反向构造“索引”。没有这个词。

关于mysql - JSON_Search() 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59292660/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com