二维码
微世推网

扫一扫关注

当前位置: 首页 » 企业商讯 » 汽车行业 » 正文

JSON数据类型_mysql从5.7之后

放大字体  缩小字体 发布日期:2022-12-28 13:21:30    作者:李中山    浏览次数:116
导读

一、概述mysql从5.7后引入了json数据类型以及json函数,可以有效得访问json格式得数据。json数据类型相对于字符串,具有以下优点:1)对于json列数据提供自动校验json格式,错误格式会提示错误;2)优化存储类型。数据以二进制方式保存,读取效率快;3)允许通过键值或者数据索引查找对象,无需把整个数据读取出来;除此之

一、概述

mysql从5.7后引入了json数据类型以及json函数,可以有效得访问json格式得数据。json数据类型相对于字符串,具有以下优点:

1)对于json列数据提供自动校验json格式,错误格式会提示错误;

2)优化存储类型。数据以二进制方式保存,读取效率快;

3)允许通过键值或者数据索引查找对象,无需把整个数据读取出来;

除此之外,json还有以下特点:

1)json存储空间大致于longblob或longtext差不多;

2)mysql8.0.13之后,json允许默认值为null;

3)json列不能设置索引,可通过json中得键值设置索引来提高查询效率;

4)json中null、true、false必须使用小写,

二、使用示例

SELECt VERSION(); -- 5.7以上才支持

建表

CREATE TABLE `point_data` ( `id` int(20) NOT NULL AUTO_INCREMENT COMMENT '自增id', `device_id` char(32) COMMENT '设备id', `uid` char(32) COMMENT '设备uid', `product_id` char(32) COMMENT '产品id', `point` json DEFAULT NULL COMMENT 'json类型得字段', `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, `status` char(1) DEFAULT '1' COMMENT '1.有效 0.无效', PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;

测试数据

insert into point_data(id,device_id,uid,product_id,point) values(1783248988,'c95f1032397248d39c175b0b30161a52','ee932240a6d3de4e65c34ba8bb79d089','b634af1c1e3b4f84bc22ceb7042c8574','{\\"brightness\\":{\\"i\\":7,\\"n\\":\\"brightness\\",\\"t\\":1,\\"v\\":\\"165\\",\\"time\\":1647928879000},\\"work_mode\\":{\\"i\\":2,\\"n\\":\\"work_mode\\",\\"t\\":1,\\"v\\":\\"198\\",\\"time\\":1647928879000},\\"music_state\\":{\\"i\\":9,\\"n\\":\\"music_state\\",\\"t\\":1,\\"v\\":\\"228\\",\\"time\\":1647928879000},\\"temp_value\\":{\\"i\\":12,\\"n\\":\\"temp_value\\",\\"t\\":4,\\"v\\":\\"21\\",\\"time\\":1647928879000},\\"warm_switch\\":{\\"i\\":11,\\"n\\":\\"warm_switch\\",\\"t\\":9,\\"v\\":\\"false\\",\\"time\\":1647928879000}}');insert into point_data(id,device_id,uid,product_id,point) values(1783248989,'c95f1032397248d39c175b0b30161a53','ee932240a6d3de4e65c34ba8bb79d080','b634af1c1e3b4f84bc22ceb7042c8574','[\\"brightness\\":{\\"i\\":7,\\"n\\":\\"brightness\\",\\"t\\":1,\\"v\\":\\"165\\",\\"time\\":1647928879000},\\"work_mode\\":{\\"i\\":2,\\"n\\":\\"work_mode\\",\\"t\\":1,\\"v\\":\\"198\\",\\"time\\":1647928879000},\\"music_state\\":{\\"i\\":9,\\"n\\":\\"music_state\\",\\"t\\":1,\\"v\\":\\"228\\",\\"time\\":1647928879000},\\"temp_value\\":{\\"i\\":12,\\"n\\":\\"temp_value\\",\\"t\\":4,\\"v\\":\\"21\\",\\"time\\":1647928879000},\\"warm_switch\\":{\\"i\\":11,\\"n\\":\\"warm_switch\\",\\"t\\":9,\\"v\\":\\"false\\",\\"time\\":1647928879000}]');insert into point_data(id,device_id,uid,product_id,point) values(1783248980,'c95f1032397248d39c175b0b30161a54','ee932240a6d3de4e65c34ba8bb79d081','b634af1c1e3b4f84bc22ceb7042c8574','{"brightness":{"i":7,"n":"brightness","t":1,"v":"165","time":1647928879000},"work_mode":{"i":2,"n":"work_mode","t":1,"v":"198","time":1647928879000},"music_state":{"i":9,"n":"music_state","t":1,"v":"228","time":1647928879000},"temp_value":{"i":12,"n":"temp_value","t":4,"v":"21","time":1647928879000},"warm_switch":{"i":11,"n":"warm_switch","t":9,"v":"false","time":1647928879000}}');insert into point_data(id,device_id,uid,product_id,point) values(1783248981,'c95f1032397248d39c175b0b30161a55','ee932240a6d3de4e65c34ba8bb79d082','b634af1c1e3b4f84bc22ceb7042c8574','["brightness":{"i":7,"n":"brightness","t":1,"v":"165","time":1647928879000},"work_mode":{"i":2,"n":"work_mode","t":1,"v":"198","time":1647928879000},"music_state":{"i":9,"n":"music_state","t":1,"v":"228","time":1647928879000},"temp_value":{"i":12,"n":"temp_value","t":4,"v":"21","time":1647928879000},"warm_switch":{"i":11,"n":"warm_switch","t":9,"v":"false","time":1647928879000}]');

查询json字段

SELECT POINT -> '$."work_mode"' work_modeFROM point_dataWHERe id = 1783248988;SELECt POINT -> '$."work_mode"."i"' work_modeFROM point_dataWHERe id = 1783248988;三、JSON函数

感谢分享dev.mysql感谢原创分享者/doc/refman/5.7/en/json-creation-functions.html

感谢分享特别cnblogs感谢原创分享者/ingxx/p/15880021.html

构建json得函数
  • JSON_ARRAY([*val*[,*val*] ...])

    将参数返回成一个JSON数组

    SELECt JSON_ARRAY(1, "abc", NULL, TRUE, CURTIME());+---------------------------------------------+| JSON_ARRAY(1, "abc", NULL, TRUE, CURTIME()) |+---------------------------------------------+| [1, "abc", null, true, "11:30:24.000000"] |+---------------------------------------------+

  • JSON_OBJECT([key, val[, key, val] ...])

    接收键值对参数,并返回成JSON对象

    SELECT JSON_OBJECT('id', 87, 'name', 'carrot');+-----------------------------------------+| JSON_OBJECT('id', 87, 'name', 'carrot') |+-----------------------------------------+| {"id": 87, "name": "carrot"} |+-----------------------------------------+

  • JSON_QUOTE(string)

    SELECT JSON_QUOTE('null'), JSON_QUOTE('"null"');+--------------------+----------------------+| JSON_QUOTE('null') | JSON_QUOTE('"null"') |+--------------------+----------------------+| "null" | "\\"null\\"" |+--------------------+----------------------+搜索JSON值得函数

  • JSON_ConTAINS(target, candidate[, path])

    判断target中指定path是否含有candidate,返回1表示包含

    SET 等j = '{"a": 1, "b": 2, "c": {"d": 4}}';SET 等j2 = '1';SELECT JSON_ConTAINS(等j, 等j2, '$.a');+-------------------------------+| JSON_ConTAINS(等j, 等j2, '$.a') |+-------------------------------+| 1 |+-------------------------------+SELECT JSON_ConTAINS(等j, 等j2, '$.b');+-------------------------------+| JSON_ConTAINS(等j, 等j2, '$.b') |+-------------------------------+| 0 |+-------------------------------+SET 等j2 = '{"d": 4}';SELECT JSON_ConTAINS(等j, 等j2, '$.a');+-------------------------------+| JSON_ConTAINS(等j, 等j2, '$.a') |+-------------------------------+| 0 |+-------------------------------+SELECT JSON_ConTAINS(等j, 等j2, '$.c');+-------------------------------+| JSON_ConTAINS(等j, 等j2, '$.c') |+-------------------------------+| 1 |+-------------------------------+

  • JSON_CONTAINS_PATH(json_doc, one_or_all, path[, path] ...)

    判断JSON文档中是否包含指定path

    'one': 1 if at least one path exists within the document, 0 otherwise.

    'all': 1 if all paths exist within the document, 0 otherwise.

    SET 等j = '{"a": 1, "b": 2, "c": {"d": 4}}';SELECT JSON_CONTAINS_PATH(等j, 'one', '$.a', '$.e');+---------------------------------------------+| JSON_CONTAINS_PATH(等j, 'one', '$.a', '$.e') |+---------------------------------------------+| 1 |+---------------------------------------------+SELECT JSON_CONTAINS_PATH(等j, 'all', '$.a', '$.e');+---------------------------------------------+| JSON_CONTAINS_PATH(等j, 'all', '$.a', '$.e') |+---------------------------------------------+| 0 |+---------------------------------------------+SELECT JSON_CONTAINS_PATH(等j, 'one', '$.c.d');+----------------------------------------+| JSON_CONTAINS_PATH(等j, 'one', '$.c.d') |+----------------------------------------+| 1 |+----------------------------------------+SELECT JSON_CONTAINS_PATH(等j, 'one', '$.a.d');+----------------------------------------+| JSON_CONTAINS_PATH(等j, 'one', '$.a.d') |+----------------------------------------+| 0 |+----------------------------------------+

  • JSON_EXTRACT(json_doc, path[, path] ...)

    根据指定path提取数据

    SELECT JSON_EXTRACT('[10, 20, [30, 40]]', '$[1]');+--------------------------------------------+| JSON_EXTRACT('[10, 20, [30, 40]]', '$[1]') |+--------------------------------------------+| 20 |+--------------------------------------------+SELECT JSON_EXTRACT('[10, 20, [30, 40]]', '$[1]', '$[0]');+----------------------------------------------------+| JSON_EXTRACT('[10, 20, [30, 40]]', '$[1]', '$[0]') |+----------------------------------------------------+| [20, 10] |+----------------------------------------------------+SELECT JSON_EXTRACT('[10, 20, [30, 40]]', '$[2][*]');+-----------------------------------------------+| JSON_EXTRACT('[10, 20, [30, 40]]', '$[2][*]') |+-----------------------------------------------+| [30, 40] |+-----------------------------------------------+

  • JSON_UNQUOTE( JSON_EXTRACT(column, path) ) JSON_UNQUOTE(column -> path) column->>path
  • JSON_EXTRACT

    SELECT JSON_EXTRACT (`point`, '$[0]') AS ONE,JSON_EXTRACT (`point`, '$[1]') twoFROM point_data;

  •  
    (文/李中山)
    打赏
    免责声明
    • 
    本文为李中山原创作品•作者: 李中山。欢迎转载,转载请注明原文出处:http://www.udxd.com/qysx/show-135320.html 。本文仅代表作者个人观点,本站未对其内容进行核实,请读者仅做参考,如若文中涉及有违公德、触犯法律的内容,一经发现,立即删除,作者需自行承担相应责任。涉及到版权或其他问题,请及时联系我们邮件:weilaitui@qq.com。
     

    Copyright©2015-2023 粤公网安备 44030702000869号

    粤ICP备16078936号

    微信

    关注
    微信

    微信二维码

    WAP二维码

    客服

    联系
    客服

    联系客服:

    24在线QQ: 770665880

    客服电话: 020-82301567

    E_mail邮箱: weilaitui@qq.com

    微信公众号: weishitui

    韩瑞 小英 张泽

    工作时间:

    周一至周五: 08:00 - 24:00

    反馈

    用户
    反馈