【问题标题】:How to extract rows from a json array using the mysql udf json_extract 0.4.0?如何使用 mysql udf json_extract 0.4.0 从 json 数组中提取行?
【发布时间】:2016-07-20 15:58:13
【问题描述】:

我有一些 sql 想要传递到 mysql 存储过程中。我正在使用 mysql-json-udfs-0.4.0-labs-json-udfs-linux-glibc2.5-x86_64 中的 json 函数。我们正在运行一个 mysql 5.5.4 服务器。可以选择更新到 5.7.x。

当我跑步时

set @mapJSON = '[{"from":12,"to":0},{"from":11,"to":-1},{"from":1,"to":1}]' ;
select json_extract(@mapJSON,'from') `from`,json_extract(@mapJSON,'to') `to` ;

期待

   from    to
   12      0
   11      -1
   1       1

我来了

    from    to
    {"from":12,"to":0}  {"from":12,"to":0}

问题是如何使用 udf json_extract 0.4.0 从 json 数组中提取行?

我暂时通过使用comma_schema和json作为解决这个问题

        {
        "map": [
            {
                "from": 12,
                "to": 0
            },
            {
                "from": 1,
                "to": 10
            },
            {
                "from": 2,
                "to": 20
            },
            {
                "from": 3,
                "to": 30
            },
            {
                "from": 4,
                "to": 40
            },
            {
                "from": 5,
                "to": 50
            },
            {
                "from": 6,
                "to": 60
            },
            {
                "from": 7,
                "to": 70
            },
            {
                "from": 8,
                "to": 80
            },
            {
                "from": 9,
                "to": 90
            },
            {
                "from": 10,
                "to": 100
            }
        ]
    }

运行后给出结果

   select `common_schema`.`extract_json_value`(@mapJSON,'/map/from') `from`,`common_schema`.`extract_json_value`(@mapJSON,'/map/to') `to` ;

作为空格分隔的字符串

    from                    to
    12 1 2 3 4 5 6 7 8 9 10 0 10 20 30 40 50 60 70 80 90 100

然后我使用 where @recommendationMapJSON 是被传递到存储过程的新 json 提取。

        create temporary table temporary_recommendation_maps AS (
            select `common_schema`.`extract_json_value`(@recommendationMapJSON,'/map/from') `from`,`common_schema`.`extract_json_value`(@recommendationMapJSON,'/map/to') `to` 
        ) ;

        create temporary table temporary_recommendation_map (
            `from` int ,
            `to` int
        ) ;

        select length(`from`) - length(replace(`from`,' ','')) +1 into @mapCount from temporary_recommendation_maps ;
        set @mapIndex = 0 ;
        while @mapIndex < @mapCount do
            select substring_index(`from`,' ',1) into @from from temporary_recommendation_maps ;
            select substring_index(`to`,' ',1) into @to from temporary_recommendation_maps ;
            insert into temporary_recommendation_map(`from`,`to`) values (@from,@to) ;
            update temporary_recommendation_maps
            set `from` = substring(`from`,instr(`from`,' ')+1) 
            , `to` = substring(`to`,instr(`to`,' ')+1) ;
            set @mapIndex =  @mapIndex + 1 ;
        end while ;
        update temporary_recommendation_maps
        set `from` = '' 
        , `to` = '' ;

它给出了我想要的地图。

    select * from temporary_recommendation_map ;

     from   to
     12 0
     1  10
     2  20
     3  30
     4  40
     5  50
     6  60
     7  70
     8  80
     9  90
     10 100

【问题讨论】:

    标签: mysql json udf


    【解决方案1】:

    试试这个代码。

    DROP TABLE IF EXISTS tmp;
    DROP PROCEDURE IF EXISTS teste;
    
    DELIMITER $$
    
    CREATE PROCEDURE teste()
    BEGIN
    
      DECLARE i      INT DEFAULT  0;
      DECLARE jCount INT DEFAULT -1;
    
      CREATE TEMPORARY TABLE tmp( ou_from INT, out_to INT );
    
      SET @mapJSON = '[{"from":12,"to":0},{"from":11,"to":-1},{"from":1,"to":1},{"a":"teste"}]' ;
    
      SET jCount = jCount + JSON_LENGTH( @mapJSON, '$' );
    
      WHILE ( i <= jCount ) DO
    
        INSERT INTO tmp( ou_from, out_to ) 
        VALUES( JSON_EXTRACT(@mapJSON, CONCAT( '$[', i, '].from') )
              , JSON_EXTRACT(@mapJSON, CONCAT( '$[', i, '].to'  ) )
              );
        SET i = i + 1;
    
      END WHILE;
    
      SELECT ou_from AS 'from', out_to AS 'to' FROM tmp;
    
      /*
      SELECT JSON_EXTRACT(@mapJSON, "$[1].from") AS 'from',
             JSON_EXTRACT(@mapJSON, "$[1].to") AS 'to' ;
    */
    
    END $$
    
    DELIMITER ;
    
    CALL teste;
    

    【讨论】:

    • 我在 mySql 5.7 中制作示例
    【解决方案2】:

    json_extract() 中的语法有点不对劲。尝试改用这个:

    SET @mapJSON = '[{"from":12,"to":0},{"from":11,"to":-1},{"from":1,"to":1}]' ;
    SELECT JSON_EXTRACT(@mapJSON, "$.from") AS `from`,
           JSON_EXTRACT(@mapJSON, "$.to") AS `to`
    

    这应该会给你一个看起来像这样的结果集:

    from         to
    [12, 11, 1]  [0, -1, 1]
    

    【讨论】:

    • 我的例子是从到 {"from":12,"to":0} {"from":12,"to":0}。
    【解决方案3】:

    使用索引获取数组值。

    $[ index ] 
    

    样本:

    SELECT JSON_EXTRACT(@mapJSON, "$[0].from") AS 'from',
             JSON_EXTRACT(@mapJSON, "$[0].to") AS 'to' ;
    

    【讨论】:

    • 这导致我的服务器上的 from to {"from":12,"to":0} {"from":12,"to":0}
    猜你喜欢
    • 1970-01-01
    • 2014-12-27
    • 2022-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多