【问题标题】:JSON_Search() returns nullJSON_Search() 返回 null
【发布时间】:2019-12-11 19:21:14
【问题描述】:

我的查询有问题。在我的数据库中,我有一个包含这种格式的 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 运行良好。 非常感谢。

【问题讨论】:

    标签: mysql sql json


    【解决方案1】:

    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] ...])

      返回 JSON 文档中给定 字符串 的路径。

    我把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]" |
    +-----------+
    

    附:请不要使用反向格式“索引”。没有这个词。

    【讨论】:

    • 过去 2 小时我一直在用头撞墙。string
    猜你喜欢
    • 1970-01-01
    • 2020-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-21
    • 2020-06-19
    • 2011-10-15
    • 2014-06-28
    相关资源
    最近更新 更多