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
  • 相关阅读:
    课堂练习-增加信息
    07-异常处理(动手动脑问题)
    课程作业1
    《豆瓣的基础架构》---阅读
    《从0到100:知乎网站架构变迁史》---阅读
    mysql里的insert
    第五周周二练习:实验 5 Spark SQL 编程初级实践
    Java链接Oracle
    余额宝技术架构及演进------阅读
    淘宝网的软件质量属性分析
  • 原文地址:https://www.cnblogs.com/jardeng/p/13970365.html
Copyright © 2011-2022 走看看