如何使用mysql udf json_extract 0.4.0从json数组中提取行? [英] How to extract rows from a json array using the mysql udf json_extract 0.4.0?

查看:754
本文介绍了如何使用mysql udf json_extract 0.4.0从json数组中提取行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have some sql that I want to pass into a mysql stored procedure. I'm using the json functions in mysql-json-udfs-0.4.0-labs-json-udfs-linux-glibc2.5-x86_64. We are running a mysql 5.5.4 server. Updating to 5.7.x is an option.

我跑步时

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数组中提取行?

The question is how to extract rows from a json array using the udf json_extract 0.4.0?

我暂时使用

I solved this for the moment by using comma_schema with json as

        {
        "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

然后我使用@recommendationMapJSON将其提取为传递到存储过程中的新json.

which I then extract using where @recommendationMapJSON is the new json being passed into the stored procedure.

        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

推荐答案

您在json_extract()中使用的语法有些偏离.尝试改用此方法:

Your syntax inside json_extract() was a bit off. Try using this instead:

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`

这应该给您一个结果集,看起来像这样:

This should give you a result set looking something like this:

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

这篇关于如何使用mysql udf json_extract 0.4.0从json数组中提取行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆