gpt4 book ai didi

MySQL json_search 数值

转载 作者:可可西里 更新时间:2023-11-01 07:08:42 41 4
gpt4 key购买 nike

我有一个类似这样的对象的 json 列表

[{
"something": "bla",
"id": 2
}, {
"something": "yes",
"id": 1
}]

我的 id 字段始终是一个数值。但是当我尝试查找 id = 2 时,MySQL 返回 NULL

select
json_search(
json_extract(
'[{"something": "bla" ,"id": 2}, {"something": "yes","id": 1}]',
"$[*].id"
),
'one',
2
) as json_search;

json_search |
------------|
|

当我在 json id 对象中使用字符串作为值而不是数值时,我得到了索引为 0 的结果。

select
json_search(
json_extract(
'[{"something": "bla" ,"id": "2"}, {"something": "yes","id": 1}]',
"$[*].id"
),
'one',
"2"
) as json_search;

json_search |
------------|
"$[0]" |

我正在使用 MySQL 5.7.17

@@version  |
-----------|
5.7.17-log |

mysql没有提供json数组的数值搜索吗?

最佳答案

您可以尝试一些复杂的、不直观的并且可能有性能问题的东西,但这是一个选择:

mysql> SELECT JSON_SEARCH(
-> REPLACE(
-> REPLACE(
-> REPLACE(
-> JSON_EXTRACT('[
'> {"something": "bla" ,"id": 2},
'> {"something": "yes","id": 1}
'> ]', "$[*].id"),
-> ', ', '","'),
-> '[', '["'),
-> ']', '"]'),
-> 'one', '2') `json_search`;
+-------------+
| json_search |
+-------------+
| "$[0]" |
+-------------+
1 row in set (0.00 sec)

关于MySQL json_search 数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44826770/

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