zoukankan      html  css  js  c++  java
  • 数据库中查询json 样式的值的sql语句

    参考:http://www.lnmp.cn/mysql-57-new-features-json.html


    方式一: 可以查到json中的Key:value

     

    SELECT * FROM EDI.edi_history WHERE JSON_CONTAINS(response_summary, json_array(json_object("orderNo","10007"))) 
    and JSON_CONTAINS(response_summary,  json_array(json_object("courierCompanyCode","Air21")))
    
    and JSON_CONTAINS(response_summary,  json_array(json_object("dataSource", "EDI_AFTERSHIP")))
    
    and JSON_CONTAINS(response_summary,  json_array(json_object("courierBillNo","888888")))

    方式2:   仅仅通过包含:

     
    select * from EDI.edi_history  where json_contains(response_summary, '{"dest_stn":"PRQ"}')

     

    只更新部分值:

    UPDATE EDI.edi_history SET response_summary = JSON_REPLACE(response_summary, '$.result', 'php') WHERE id = 1;

    创建表

    CREATE TABLE t_json(id INT PRIMARY KEYNAME VARCHAR(20) , info  JSON);
     
    插入记录
    INSERT INTO t_json(id,sname,info) VALUES(1 ,'test','{"time":"2017-01-01 13:00:00","ip":"192.168.1.1","result":"fail"}');
    INSERT INTO t_json(id,sname,info)  VALUES(2 ,'my',JSON_OBJECT("time",NOW(),'ip','192.168.1.1','result','fail'));
     
    查询IP键
    SELECT sname,JSON_EXTRACT(info,'$.ip'FROM t_json;
    查询有多少个键
    SELECT id,json_keys(info) AS "keys" FROM t_json;
     
     
    删除键
    UPDATE t_json SET info = json_remove(info,'$.ip');
    增加键
    UPDATE t_json SET info = json_set(info,'$.ip','192.168.1.1');
    变更值
    UPDATE t_json SET info = json_set(info,'$.ip','192.168.1.2');
  • 相关阅读:
    UE4 Couldn't find target rules file for target
    ue4 源码编译 虚拟内存不足 堆限制
    Windows RawInputPluginForUE4(2)
    maven的用法
    svn出现subclipse不兼容或不存在
    cglib
    Spring中bean中scope属性的意义
    org.springframework.beans.NotWritablePropertyException: Invalid property
    sql server 中的存储过程
    如何将数据库文件.mdf导入/恢复到SQL_Server2000
  • 原文地址:https://www.cnblogs.com/lshan/p/9184630.html
Copyright © 2011-2022 走看看