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
  • 相关阅读:
    Extract Manifest File From Application (exe)
    Basis: Command and Immediate Window
    MVC框架啊
    Brian's Guide to Solving Any Perl Problem
    [bbk3106]第13集 Chapter 07 介绍oracle的asm存储设备
    ASM
    [bbk3105]第12集 Chapter 07 介绍oracle的asm存储设备
    [bbk3104]第11集 Chapter 07 介绍oracle的asm存储设备
    [bbk3103]第10集 Chapter 06 介绍RAC的体系结构
    手把手一起安装RAC
  • 原文地址:https://www.cnblogs.com/jardeng/p/13970365.html
Copyright © 2011-2022 走看看