我正在寻找一个纯MySQL的单一查询。此查询的目标是利用诸如 SUBSTRING_INDEX
之类的内容。 , CONCAT
,或者任何需要的东西,以便在字符串中查找值。
假设该字符串看起来像这样:
{"name":34,"otherName":55,"moreNames":12,"target":26,"hello":56,"hi":26,"asd":552,"p":3722,"bestName":11,"cc":6,"dd":10,}
我的目标是获得 target
的值,在这种情况下,26
。然而,"target":26
可能并不总是位于字符串中的该位置。其他任何属性也不会。最重要的是,该值可能并不总是 26
。我需要某种方法来检查 "target":
之后的数字但在,
之前之后"target":
。有什么办法可以做到这一点吗?
最佳答案
这个?
create table sandbox (id integer, jsoncolumn varchar(255));
insert into sandbox values (1,'{"name":34,"otherName":55,"moreNames":12,"target":26,"hello":56,"hi":26,"asd":552,"p":3722,"bestName":11,"cc":6,"dd":10}');
mysql root@localhost:sandbox> SELECT jsoncolumn->'$.target' from sandbox;
+--------------------------+
| jsoncolumn->'$.target' |
|--------------------------|
| 26 |
+--------------------------+
https://dev.mysql.com/doc/refman/5.7/en/json-search-functions.html
关于mysql - 在纯 MySQL 中从 JSON 字符串获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47944299/