我都快疯了。有没有人能解释一下,为什么第一次调用会返回路径,但是从表中选择路径时却找不到路径?
(MySQL-版本5.7.20)
按照预期工作:
SELECT JSON_SEARCH(
'{"mm": [{"id":"1","field":"test","value":33}]}',
'one', '1', null, '$.mm[*].id') as path;
-- output: path== "$.mm[0].id"不工作:
drop table test;
create table test(
id int primary key,
data json
);
insert into test (id, data) values (1, '{}');
update test set data=JSON_SET(data, '$.mm', json_array()) where id=1;
update test set data=JSON_ARRAY_APPEND(data, '$.mm', '{"id":"1","field":"test","value":33}') where id=1;
SELECT JSON_SEARCH(data, 'one', '1', null, '$.mm[*].id') as path from test where id=1;
-- output: path==NULL
-- expected: path== "$.mm[0].id"我很确定我做错了什么,但我不知道是哪一个...
谢谢
发布于 2017-11-17 17:09:29
明白了..。为了在表中具有对象类型,必须将字符串强制转换为json。这对于append和set是正确的。在使用JSON_SEARCH时,如果第一个参数是字符串,则会自动转换为json。
https://stackoverflow.com/questions/47346022
复制相似问题