当前位置:网站首页> 关于mysql中的json解析函数JSON_EXTRACT
关于mysql中的json解析函数JSON_EXTRACT
2022-07-05 16:42:00 【1024问】
mysql json解析函数JSON_EXTRACT
例子
mysql5.7 json格式与json_extract方法
数据初始化
select语句
返回结果解析
用法解析
mysql json解析函数JSON_EXTRACTMYSQl自带的解析函数JSON_EXTRACT,用JSON_EXTRACT函数解析出来的函数会包含双引号
例子t_table表里面source字段是以json格式写的值为
{ "info" : { "color" : "白色", "inner_color" : "米色", "number" : "12345678", "registration_date" : "2012-11" },"accessory" : [ "1", "4", "5", "6", "7", "8", "9", "10" ],"remark" : "测试"}
查询color不去掉双引号,inner_color去掉双引号,remark去掉双引
select source->'$.info.color' as color,replace(source->'$.info.inner_color','"','') as inner_color,replace(source->'$.remark','"','') as remark from t_table
查询结果
“白色” | 米色 | 测试 |
json_test表数据,id和jsonstr字段(类型json)
{ "no": "7", "title": "运动方式", "content": [{ "text": "您平时经常进行的运动及时间", "item1": [{ "text": "慢跑 / 快走 / 走跑结合", "type": "select", "value": "selected" }, { "text": "上下楼梯 / 爬山", "type": "multselect", "value": "selected" }], "item2": [{ "text": "慢跑222走跑结合", "type": "text", "value": "慢跑2" }, { "text": "上下楼梯 / 爬山2", "type": "number", "value": "33" }] }]}
select语句SELECT id, json_extract( t.jsonstr, '$.*' ), json_extract( t.jsonstr, '$.title' ) AS "title", json_extract( t.jsonstr, '$.content' ) AS "content" , json_extract( t.jsonstr, '$**.text' ) AS "text" , json_extract( t.jsonstr, '$.content[*].item1[*]' ) AS "item1" FROM json_test t;
返回结果解析//json_extract( t.jsonstr, '$.*' )返回:["7", "运动方式", [{"text": "您平时经常进行的运动及时间", "item1": [{"text": "慢跑 / 快走 / 走跑结合", "type": "select", "value": "selected"}, {"text": "上下楼梯 / 爬山", "type": "multselect", "value": "selected"}], "item2": [{"text": "慢跑222走跑结合", "type": "text", "value": "慢跑2"}, {"text": "上下楼梯 / 爬山2", "type": "number", "value": "33"}]}]]//json_extract( t.jsonstr, '$.title' ) AS "title"返回:"运动方式"//json_extract( t.jsonstr, '$.content' ) AS "content" 返回:[{"text": "您平时经常进行的运动及时间", "item1": [{"text": "慢跑 / 快走 / 走跑结合", "type": "select", "value": "selected"}, {"text": "上下楼梯 / 爬山", "type": "multselect", "value": "selected"}], "item2": [{"text": "慢跑222走跑结合", "type": "text", "value": "慢跑2"}, {"text": "上下楼梯 / 爬山2", "type": "number", "value": "33"}]}]//json_extract( t.jsonstr, '$**.text' ) AS "text" 返回:["您平时经常进行的运动及时间", "慢跑 / 快走 / 走跑结合", "上下楼梯 / 爬山", "慢跑222走跑结合", "上下楼梯 / 爬山2"]//json_extract( t.jsonstr, '$.content[*].item1[*]' ) AS "item1" 返回:[{"text": "慢跑 / 快走 / 走跑结合", "type": "select", "value": "selected"}, {"text": "上下楼梯 / 爬山", "type": "multselect", "value": "selected"}]
用法解析‘$.*’ | 返回全部json |
‘$.title’ | 返回key=”title”的数据 |
‘$**.text’ | 返回所有最底层key=”text”的数据 |
‘$.content[*].item1[*]’ | 返回key=content的list的key=item1的list的所有内容 |
官方文档:https://dev.mysql.com/doc/refman/5.7/en/json.html
以上为个人经验,希望能给大家一个参考,也希望大家多多支持软件开发网。
边栏推荐
- 高数 | 旋转体体积计算方法汇总、二重积分计算旋转体体积
- Games101 notes (I)
- Learnopongl notes (II) - Lighting
- 关于new Map( )还有哪些是你不知道的
- C language to get program running time
- 2022 年 Q2 加密市场投融资报告:GameFi 成为投资关键词
- Using C language to realize palindrome number
- The first EMQ in China joined Amazon cloud technology's "startup acceleration - global partner network program"
- C# TCP如何限制单个客户端的访问流量
- [729. My schedule I]
猜你喜欢
随机推荐
Embedded-c Language-3
It is forbidden to copy content JS code on the website page
Use JDBC technology and MySQL database management system to realize the function of course management, including adding, modifying, querying and deleting course information.
[wechat applet] read the life cycle and route jump of the applet
手机开证券账户安全吗?怎么买股票详细步骤
时间戳strtotime前一天或后一天的日期
Machine learning compilation lesson 2: tensor program abstraction
Learnopongl notes (I)
The survey shows that the failure rate of traditional data security tools in the face of blackmail software attacks is as high as 60%
Rider 设置选中单词侧边高亮,去除警告建议高亮
URP下Alpha从Gamma空间到Linner空间转换(二)——多Alpha贴图叠加
Writing method of twig array merging
Practical example of propeller easydl: automatic scratch recognition of industrial parts
Keras crash Guide
C how TCP restricts the access traffic of a single client
Thoughtworks 全球CTO:按需求构建架构,过度工程只会“劳民伤财”
调查显示传统数据安全工具面对勒索软件攻击的失败率高达 60%
高数 | 旋转体体积计算方法汇总、二重积分计算旋转体体积
Detailed explanation of printf() and scanf() functions of C language
npm安装