• mysql json


    mysql5.7版本支持json,具体版本5.7.8开始。

    背景:公司实用json存储数据结构,上线新功能,更改历史数据,研究下mysql-json。

    #建表
    CREATE TABLE user(id INT PRIMARY KEY, name VARCHAR(20) , $json JSON);
    #插入数据
    INSERT INTO user VALUES(1,"zhangsan",'{"time":"2018-06-05 15:40:00","action":"ceshi","result":"success","falg":0,"array":[11,12],"other":{"make1":"ceshi","make2":"ceshi"}}');

    插入json 最后增加
    update user set $json = json_array_insert ($json,"$.other[2]",JSON_OBJECT('make3', 'ceshi3', JSON_OBJECT('ids',JSON_ARRAY(), 'isAll', true)))
    
    
    插入json 数组中间
    update user 
        set $json = json_array_insert ($json,"$.other[1]",JSON_OBJECT('make2.5', 'ceshi2.5'))
    删除json数组中间
    update user set $json = JSON_REMOVE($json,"$.other[2]") 
    查询包含字符并列出
    select json_extract($json,'$.action') from user 
    API:
    分类 函数 描述
    创建json
    json_array 创建json数组  json_array() 返回空数组 []
    json_object 创建json对象
    json_quote 将json转成json字符串类型
    查询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,返回匹配的路径
    修改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类型
    返回json属性 
    json_depth 返回json文档的最大深度
    json_length 返回json文档的长度
    json_type 返回json值得类型
    json_valid 判断是否为合法json文档
    备注:
    1、对于json_insert和json_replace来说一般情况没必要针对数组使用。
    2、mysql5.7.9开始增加简写方式:column->path
    3、只有json_extract和json_search中的path才支持通配,其他json_set,json_insert等都不支持。

     

    对于key值得判断也提供了这样一个函数——json_contains_path(json_doc, one_or_all, paths)
    
    1.返回值:对于这种判断类型的函数返回的一般都是true or false 或者 1 和 0。这个函数也如此,在mysql返回的是1和0。
    
    2.参数分析:json_doc顾名思义就是json数据;paths是指要找的key,可以传入多个的key参数;one_or_all指一个值是one表示找出paths参数中的任意一个,all表示找出全部。

       select JSON_CONTAINS_PATH('{"a":1,"b":1}', 'one','$.a','$.c')
       结果1。只要存在一个

       select JSON_CONTAINS_PATH('{"a":1,"b":1}', 'all','$.a','$.c')
       结果0。必须全部存在。

     对于value得判断 json_contains(json_doc, paths)
    select * from form_specification where json_contains(JSON_EXTRACT($json, "$.components[*].editable"),'false')

       select * from form_specification where json_contains(JSON_EXTRACT($json, "$.components"),'{"editable": false}') 

       select * from form_specification where json_contains(JSON_EXTRACT($json, "$.components[*]"),'[{"editable": false},{"type": "text"}]')

    
    
  • 相关阅读:
    SHELL基础
    阿里
    Ansible基础
    js实现的跳转页面方法实现汇总
    绕过js-sdk,微信转发的时候在标题添加时间和地点。
    wechat-js-sdk
    js调用百度地图api实现定位
    微创网站工作总结:用错地方的资源
    项目进行时—整理
    js实现双击改变文本内容
  • 原文地址:https://www.cnblogs.com/start-fxw/p/10109133.html
Copyright © 2020-2023  润新知