zoukankan      html  css  js  c++  java
  • mysql 查询json

    ------------------------ 数组 ------------------------

    > 数组提取

    -- ------------------------------ 提取 JSON_EXTRACT ------------------------------
    -- 定义数组
    SET @mapJSON = '[{"id":"10000004555096","name":"1.docx","format":"docx","type":0},{"id":"10000004555098","name":"2.doc","format":"doc","type":0},{"id":"10000004555097","name":"3.html","format":"html","type":1}]';
    
    -- 提取数组下标为 0 的元素
    SELECT JSON_EXTRACT(@mapJSON, "$[0]") AS result;
    -- {"id": "10000004555096", "name": "1.docx", "type": 0, "format": "docx"}
    
    -- 提取数组每个元素的字段 name
    SELECT JSON_EXTRACT(@mapJSON, "$[*].name") AS result;
    -- ["1.docx", "2.doc", "3.html"]
    
    -- 提取数组下标为 1 的元素的字段 name
    SELECT JSON_EXTRACT(@mapJSON, "$[1].name") AS result;
    -- "2.doc"
    
    -- 提取数组下标为 2 的元素的字段 type
    SELECT JSON_EXTRACT(@mapJSON, "$[2].type") AS result;
    -- 1

    > 数组查找

    -- ------------------------------ 查找 JSON_CONTAINS ------------------------------
    -- 定义数组
    SET @mapJSON = '[{"id":"10000004555096","name":"1.docx","format":"docx","type":0},{"id":"10000004555098","name":"2.doc","format":"doc","type":0},{"id":"10000004555097","name":"3.html","format":"html","type":1}]';
    
    -- 示例1:查找 id 等于 10000004555096 的元素
    SELECT JSON_CONTAINS(JSON_EXTRACT(@mapJSON, "$[*]"), JSON_OBJECT('id', '10000004555096')) AS result;
    -- 1
    
    -- 示例1:查找 name 等于 3.htm1 的元素
    SELECT JSON_CONTAINS(JSON_EXTRACT(@mapJSON, "$[*].name"), '"3.html"') AS result;
    -- 1
    SELECT JSON_CONTAINS(JSON_EXTRACT(@mapJSON, "$[*].name"), '"3.HTML"') AS result;
    -- 0
  • 相关阅读:
    后端MVC与前端MVVM的区别
    解析身份证号
    awk的执行方式
    案例九:shell脚本自动创建多个新用户,并设置密码
    一键配置tomcat定期日志清理功能
    date命令
    将dict.define转化成dict.txt
    FastDFS 配置 Nginx 模块及访问测试
    nohup命令的用法
    手把手教你如何玩转消息中间件(ActiveMQ)
  • 原文地址:https://www.cnblogs.com/jardeng/p/13970365.html
Copyright © 2011-2022 走看看