简介
MySQL中,直至 5.7.8 版本中,才正式引入 JSON数据类型,采用一种允许快速读取文本元素(document elements)的内部二进制(internal binary)格式。
在JSON列插入或者更新的时候,将会自动验证是否是合法的JSON,如果是非法的就会执行不成功,并报错。
为什么要用JSON类型
当我们需要变更一些需求的时候,可以通过增加表字段来现实,但是随着表的增加,增加字段越來越困难。
如果我们采用 一个JSON字段,来存储一些信息,就可以比较灵活地实现动态增加或者减少字段,完成业务需求。
MySQL JSON相关函数
MySQL在推出JSON数据类型的时候,也增加了一些函数的支持,让我们快速查询到需要的数据。
1、创建JSON
- JSON_ARRAY 创建JSON数组
- JSON_OBJECT 创建JSON对象
- JSON_QUOTE 将JSON转成JSON字符串类型
#pgc-card .pgc-card-href { text-decoration: none; outline: none; display: block; width: 100%; height: 100%; } #pgc-card .pgc-card-href:hover { text-decoration: none; } /*pc 样式*/ .pgc-card { box-sizing: border-box; height: 164px; border: 1px solid #e8e8e8; position: relative; padding: 20px 94px 12px 180px; overflow: hidden; } .pgc-card::after { content: " "; display: block; border-left: 1px solid #e8e8e8; height: 120px; position: absolute; right: 76px; top: 20px; } .pgc-cover { position: absolute; width: 162px; height: 162px; top: 0; left: 0; background-size: cover; } .pgc-content { overflow: hidden; position: relative; top: 50%; -webkit-transform: translateY(-50%); transform: translateY(-50%); } .pgc-content-title { font-size: 18px; color: #222; line-height: 1; font-weight: bold; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } .pgc-content-desc { font-size: 14px; color: #444; overflow: hidden; text-overflow: ellipsis; padding-top: 9px; overflow: hidden; line-height: 1.2em; display: -webkit-inline-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; } .pgc-content-price { font-size: 22px; color: #f85959; padding-top: 18px; line-height: 1em; } .pgc-card-buy { width: 75px; position: absolute; right: 0; top: 50px; color: #406599; font-size: 14px; text-align: center; } .pgc-buy-text { padding-top: 10px; } .pgc-icon-buy { height: 23px; width: 20px; display: inline-block; background: url(https://lf1-cdn-tos.bytescm.com/obj/cdn-static-resource/pgc/v2/pgc_tpl/static/image/commodity_buy_f2b4d1a.png); } mysql8云数据库产品服务开启个人云数据库数据更安全更稳定 ¥100 购买
2、查询JSON
- JSON_CONTAINS 判断是否包含某个JSON值
- JSON_CONTAINS_PATH 判断某个路径下是否包JSON值
- JSON_EXTRACT 提取JSON值
- COLUMN->PATH JSON_EXTRACT的简洁写法,MYSQL 5.7.9开始支持
- COLUMN->>PATH JSON_UNQUOTE(COLUMN -> PATH)的简洁写法
- JSON_KEYS 提取JSON中的键值为JSON数组
- JSON_SEARCH 按给定字符串关键字搜索JSON,返回匹配的路径
3、修改JSON
- JSON_APPEND 废弃,MYSQL 5.7.9开始改名为JSON_ARRAY_APPEND
- JSON_ARRAY_APPEND 末尾添加数组元素,如果原有值是数值或JSON对象,则转成数组后,再添加元素
- JSON_ARRAY_INSERT 插入数组元素
- JSON_INSERT 插入新值,但不替换已经存在的旧值
- JSON_MERGE 合并JSON数组或对象
- JSON_REMOVE 删除JSON数据
- JSON_REPLACE 替换已经存在的旧值
- JSON_SET 替换旧值,并插入不存在的新值
- JSON_UNQUOTE 去除JSON字符串的引号,将值转成STRING类型
#pgc-card .pgc-card-href { text-decoration: none; outline: none; display: block; width: 100%; height: 100%; } #pgc-card .pgc-card-href:hover { text-decoration: none; } /*pc 样式*/ .pgc-card { box-sizing: border-box; height: 164px; border: 1px solid #e8e8e8; position: relative; padding: 20px 94px 12px 180px; overflow: hidden; } .pgc-card::after { content: " "; display: block; border-left: 1px solid #e8e8e8; height: 120px; position: absolute; right: 76px; top: 20px; } .pgc-cover { position: absolute; width: 162px; height: 162px; top: 0; left: 0; background-size: cover; } .pgc-content { overflow: hidden; position: relative; top: 50%; -webkit-transform: translateY(-50%); transform: translateY(-50%); } .pgc-content-title { font-size: 18px; color: #222; line-height: 1; font-weight: bold; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } .pgc-content-desc { font-size: 14px; color: #444; overflow: hidden; text-overflow: ellipsis; padding-top: 9px; overflow: hidden; line-height: 1.2em; display: -webkit-inline-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; } .pgc-content-price { font-size: 22px; color: #f85959; padding-top: 18px; line-height: 1em; } .pgc-card-buy { width: 75px; position: absolute; right: 0; top: 50px; color: #406599; font-size: 14px; text-align: center; } .pgc-buy-text { padding-top: 10px; } .pgc-icon-buy { height: 23px; width: 20px; display: inline-block; background: url(https://lf1-cdn-tos.bytescm.com/obj/cdn-static-resource/pgc/v2/pgc_tpl/static/image/commodity_buy_f2b4d1a.png); } 【当当】MySQL数据库应用从入门到精通(含盘) ¥44.61 购买
4、返回JSON属性
- JSON_DEPTH 返回JSON文档的zui大深度
- JSON_LENGTH 返回JSON文档的长度
- JSON_TYPE 返回JSON值的类型
- JSON_VALID 判断是否为合法JSON文档
大小限制
JSON字段的大小是受max_allowed_packet这个参数的限制,不能超过这个参数的大小,单位是byte。
总结
JSON的应用,使得我们的业务上的更新更加方便快捷。
版权声明:本文图片和内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送联系客服 举报,一经查实,本站将立刻删除,转转请注明出处:https://www.soutaowang.com/15676.html