【发布时间】:2017-08-29 18:42:48
【问题描述】:
我将 JSON_EXTRACT 与 MYSQL 一起使用并使用此命令:
SET @j = '{"id" : "1"}';
SELECT JSON_EXTRACT(@j, '$.id')
结果是
"1"
但是,当我使用
SET @j = '[{"id" : "1"}, {"id" : "2"}]';
SELECT JSON_EXTRACT(@j, '$.id')
结果是
NULL
我期待这个结果
"1"
"2"
有什么建议吗?我想要来自 JSON 的 ID 列表。
【问题讨论】:
-
您正在使用两种不同类型的对象。第一个是具有
id属性的对象。第二个是数组。您需要先遍历数组才能访问每个 id -
If it is possible that those arguments could return multiple values, the matched values are autowrapped as an array, in the order corresponding to the paths that produced them.,见JSON_EXTRACT。见db-fiddle。