MySQL中的JSON类型
json是一个非常好用的数据类型,在mysql5.7之前人们都是用string来储存json的,但是这样有个问题就是不能操作json,5.7以后就可以直接操作json数据了。
- 创建没什么好说的就是json 不用带长度默认为0
- 更新 、和插入可以用string插入
- 查询、个人不喜欢把数据交给后台处理,那就交给数据库处理吧例子如下:
- 数据库某张表有个content_json字段,里面有这样的数据!
{ "bill": [ { "bill": [ { "id": "C81AEAD03F90000142E81B405F6FADC0", "uuid": "cfd872541b3b4507a50c2b2eda6bef28", "billid": "kjdzyljgmzsfzypj", "pageno": [], "billver": "V1", "billname": "新增测试", "fjNumber": "", "trueName": "", "allPageno": [ { "top": 13, "left": 7 } ], "billValue": {}, "isOtherZL": "", "billNumber": "", "fjTMNumber": "" }, { "id": "C81AED69D6400001A07818551D9EBEC0", "uuid": "05d87c8052cd44209c684407d200b7ec", "billid": "opztsfpsgd", "pageno": [], "billver": "V1", "billname": "发票申购", "fjNumber": "", "trueName": "", "allPageno": [ { "top": 13, "left": 7 } ], "isOtherZL": "", "billNumber": "", "fjTMNumber": "" } ], "index": "", "dependBjBill": { "formula": "", "keyView": "" }, "codeCondition": { "formula": "", "keyView": "" }, "billRuleCondition": { "formula": "", "keyView": "" } }, { "bill": [ { "id": "C81AED84903000019B29EAB0196014CE", "uuid": "0d93fe614d09489cbad8fa5a3d160289", "billid": "kjdzcwgwht", "pageno": [], "billver": "V1", "billname": "财务顾", "fjNumber": "", "trueName": "", "allPageno": [ { "top": 39, "left": 7 } ], "isOtherZL": "", "billNumber": "", "fjTMNumber": "" } ], "index": "", "dependBjBill": { "formula": "", "keyView": "" }, "codeCondition": { "formula": "", "keyView": "" }, "billRuleCondition": { "formula": "", "keyView": "" } } ], "questions": [], "relyonCondition": {} }
登录后复制
结构是这样的,我们要查询最底层uuid!按条件查bill下是一个数组 里面的bill还是一个数组那么要查最底层的uuid怎么查呢?
;
个人更喜欢用类似lambda这种方法毕竟这是一个有逼格又好看的操作:
SELECT content_json->'$.bill[*].bill[*].uuid' from b WHERE JSON_CONTAINS(content_json->'$.bill[*].bill[*].uuid' ,'["cfd872541b3b4507a50c2b2eda6bef28"]')
登录后复制
查询结果
代码分析:
content_json->’$.bill[].bill[].uuid’ ;
表示content_json字段
"$"是必须符号 点代表对象
*代表全部
那么代码意思就是查询content_json字段里面全部bill(第一个)中全部bill(第二个)的uuid组成一个数组返回;
where后面跟着的条件的意思是 存在某个值 这个值可以是一个也可以是多个
JSON_CONTAINS(字段,参数);
字段必须是要查的指定值如 content_json->’$.bill[].bill[].uuid’ ;
参数必须是json类型 string是不行的。
推荐教程:mysql视频教程
以上就是mysql有没有json类型?的详细内容,更多请关注Work网其它相关文章!