MySQL 版本 5.7
鉴于 h.ent_rollup
长度始终 >=4,并且我想获取仅包含单个数组的 JSON 数据字段中倒数第二个数组元素:
ent_rollup
的示例 JSON 值:
["15","26","57","28","89","10","11","12"]
示例:我想要这个
SELECT h.`ent_rollup`->>CONCAT('"$[',(JSON_LENGTH(h.`ent_rollup`))-2,']"') FROM hierarchy h
翻译成这个
SELECT h.`ent_rollup`->>"$[6]" FROM hierarchy h
我想恢复 11
但收到此错误:
Error Code: 1064
You have an error in your SQL syntax; check the manual that corresponds to
your MySQL server version for the right syntax to use near 'concat('"$[',
(json_length(h.`ent_rollup`))-2,']"') from hierarchy h
where h.`en' at line 1
最佳答案
当您使用 ->>
时,路径必须是文字表达式(尽管文档没有明确说明)。如果要计算路径,请使用 JSON_EXTRACT()
函数。
路径中还有额外的 "
字符,这会导致 MySQL 失败(当我在 rextester.com 上尝试时,等待响应超时)。
SELECT JSON_UNQUOTE(JSON_EXTRACT(h.ent_rollup, CONCAT('$[',JSON_LENGTH(h.ent_rollup)-2,']')))
FROM hierarchy h;
关于mysql - 如何在 MySQL 的 JSON 路径中使用 CONCAT?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52043101/