文章目录 插入JSON 检索JSON JSON函数 1。优雅浏览 2。查找 3。修改 3。1。JSONSET() 3。2。JSONINSERT() 3。3。JSONREPLACE() 4。删除 5。其他函数 此学习文是基于MySQL8。0写的 得益于大神朋友的悉心指导解决不少坑,才写出此文,向大神奉上膝盖 要在MySQL中存储数据,就必须定义数据库和表结构(schema),这是一个主要的限制。为了应对这一点,从MySQL5。7开始,MySQL支恃了JavaScript对象表示(JavaScriptObjectNotation,JSON)数据类型。之前,这类数据不是单独的数据类型,会被存储为字符串。新的JSON数据类型提供了自动验证的JSON文档以及优化的存储格式。 JSON文档以二进制格式存储,它提供以下功能: 对文档元素的快速读取访问。 当服务器再次读取JSON文档时,不需要重新解析文本获取该值。 通过键或数组索引直接查找子对象或嵌套值,而不需要读取文档中的所有值。 创建一个测试表 mysqlcreatetableemployees。empdetails( empnointprimarykey, detailsjson ); QueryOK,0rowsaffected(0。17sec) mysqldescemployees。empdetails; FieldTypeNullKeyDefaultExtra empnoint(11)NOPRINULL detailsjsonYESNULL 2rowsinset(0。00sec) 检索JSON 可以使用和运算符检索JSON列的字段: mysqlselectempno,details。address。pinpin fromemployees。empdetails; empnopin 1560103 1rowinset(0。00sec) 如果不用引号检索数据,可以使用运算符(推荐此方式) mysqlselectempno,details。address。pinpin fromemployees。empdetails; empnopin 1560103 1rowinset(0。00sec) JSON函数 MySQL提供了许多处理JSON数据的函数,让我们看看最常用的几种函数。 1。优雅浏览 想要以优雅的格式显示JSON值,请使用JSONPRETTY()函数 mysqlselectempno,jsonpretty(details) fromemployees。empdetailsG 1。row empno:1 jsonpretty(details):{ email:abcexample。com, phone:11800000000, address:{ pin:560103, city:Bangalore, line1:abc, line2:xyzstreet }, location:IN } 1rowinset(0。00sec) 2。查找 可以在WHERE子句中使用colpath运算符来引用JSON的某一列 mysqlselectempno,details fromemployees。empdetails wheredetails。address。pin560103; empnodetails 1{email:abcexample。com,phone:11800000000,address:{pin:560103,city:Bangalore,line1:abc,line2:xyzstreet},location:IN} 1rowinset(0。00sec) 也可以使用JSONCONTAINS函数查询数据。如果找到了数据,则返回1,否则返回0 mysqlselectjsoncontains(details。address。pin,560103) fromemployees。empdetails; jsoncontains(details。address。pin,560103) 1 1rowinset(0。00sec) 如何查询一个key?使用JSONCONTAINSPATH函数检查address。line1是否存在 mysqlselectjsoncontainspath(details,one,。address。line1) fromemployees。empdetails; jsoncontainspath(details,one,。address。line1) 1 1rowinset(0。00sec) one表示至少应该存在一个键,检查address。line1或者address。line2是否存在 mysqlselectjsoncontainspath(details,one,。address。line1,。address。line2) fromemployees。empdetails; jsoncontainspath(details,one,。address。line1,。address。line2) 1 1rowinset(0。00sec) 如果要检查address。line1或者address。line5是否同时存在,可以使用all,而不是one mysqlselectjsoncontainspath(details,all,。address。line1,。address。line5) fromemployees。empdetails; jsoncontainspath(details,all,。address。line1,。address。line5) 0 1rowinset(0。00sec) 3。修改 可以使用三种不同的函数来修改数据:JSONSET()、JSONINSERT()和JSONREPLACE()。在MySQL8之前的版本中,我们还需要对整个列进行完整的更新,这并不是最佳的方法。 3。1。JSONSET() 替换现有值并添加不存在的值 mysqlupdateemployees。empdetails setdetailsjsonset(details,。address。pin,560100,。nickname,kai) whereempno1; QueryOK,1rowaffected(0。01sec) Rowsmatched:1Changed:1Warnings:0 mysqlselectempno,jsonpretty(details) fromemployees。empdetailsG 1。row empno:1 jsonpretty(details):{ email:abcexample。com, phone:11800000000, address:{ pin:560100, city:Bangalore, line1:abc, line2:xyzstreet }, location:IN, nickname:kai } 1rowinset(0。00sec) 3。2。JSONINSERT() 插入值,但不替换现有值 在这种情况下,。address。pin不会被更新,只会添加一个新的字段。address。line4 mysqlupdateemployees。empdetails setdetailsjsoninsert(details,。address。pin,560132,。address。line4,AWing) whereempno1; QueryOK,1rowaffected(0。01sec) Rowsmatched:1Changed:1Warnings:0 mysqlselectempno,jsonpretty(details) fromemployees。empdetailsG 1。row empno:1 jsonpretty(details):{ email:abcexample。com, phone:11800000000, address:{ pin:560100, city:Bangalore, line1:abc, line2:xyzstreet, line4:AWing }, location:IN, nickname:kai } 1rowinset(0。01sec) 3。3。JSONREPLACE() 仅替换现有值 在这种情况下,。address。line5不会被添加,只有。address。pin会被更新 mysqlupdateemployees。empdetails setdetailsjsonreplace(details,。address。pin,560132,。address。line5,Landmark) whereempno1; QueryOK,1rowaffected(0。00sec) Rowsmatched:1Changed:1Warnings:0 mysqlselectempno,jsonpretty(details) fromemployees。empdetailsG 1。row empno:1 jsonpretty(details):{ email:abcexample。com, phone:11800000000, address:{ pin:560132, city:Bangalore, line1:abc, line2:xyzstreet, line4:AWing }, location:IN, nickname:kai } 1rowinset(0。00sec) 4。删除 JSONREMOVE能从JSON文档中删除数据 mysqlupdateemployees。empdetails setdetailsjsonremove(details,。address。line4) whereempno1; QueryOK,1rowaffected(0。01sec) Rowsmatched:1Changed:1Warnings:0 mysqlselectempno,jsonpretty(details) fromemployees。empdetailsG 1。row empno:1 jsonpretty(details):{ email:abcexample。com, phone:11800000000, address:{ pin:560132, city:Bangalore, line1:abc, line2:xyzstreet }, location:IN, nickname:kai } 1rowinset(0。00sec) 5。其他函数 JSONKEYS():获取JSON文档中的所有键 mysqlselectjsonkeys(details),jsonkeys(details。address) fromemployees。empdetails whereempno1; jsonkeys(details)jsonkeys(details。address) 〔email,phone,address,location,nickname〕〔pin,city,line1,line2〕 1rowinset(0。00sec) JSONLENGTH():给出JSON文档中的元素数 mysqlselectjsonlength(details),jsonlength(details。address) fromemployees。empdetails whereempno1; jsonlength(details)jsonlength(details。address) 54 1rowinset(0。00sec) 原文链接:https:blog。csdn。netnangy2514articledetails98490082