场景
假如在 mysql
数据表 detail
中有一个 json
字段 result
,字段里的内容是这样的:
{
"status": true,
"msg": "success",
"data": [
{
"name": "华宇大药房",
"address": "北京市朝阳区顺义路115号"
}
]
}
要在查询时直接提取 address
的内容。
code
这么写 mysql
查询语句:
select
id,
JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(JSON_EXTRACT(result, "$.data"), '$[0]'), '$.address')) as address
from
detail;
输出:
mysql> select id, JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(JSON_EXTRACT(result, "$.data"), '$[0]'), '$.address')) as address from detail;
+----+-------------------------+
| id | address |
+----+-------------------------+
| 1 | 北京市朝阳区顺义路115号 |
+----+-------------------------+
1 row in set (0.00 sec)