Extract json records to rows in MySQL 5.7, like PostgreSQL json_each function

91 Views Asked by At

We have one business scenario: each id has sevaral codes, each code has "current, max, min" value. But each id's code are not same, so we use json to store them in PostgreSQL 12 {"code1":[cur,min,max], "code2":[cur,min,max],..."

We want to extract these json for analysis, so we use json_each() to extract key-value in rows

The problems is: We should realize same function in MySQL 5.7, no json_table() or json_each()

here are example in PostgreSQL:

test=# create table test (id int, data json);
CREATE TABLE


test=# insert into test values (1, '{"1":[0,0,1], "2":[0,1,0]}'), (2, '{"2":[0,0,1], "3":[0,1,0], "4":[0,0,0]}');
INSERT 0 2


test=# select * from test;
 id |                    data
----+-----------------------------------------
 1  | {"1":[0,0,1], "2":[0,1,0]}
 2  | {"2":[0,0,1], "3":[0,1,0], "4":[0,0,0]}


test=# select id, key::int as code, value->>0 as cur_val, value->>1 as min_val, value->>2 as max_val from test, json_each(data);
 id | code | cur_val | min_val | max_val
----+------+---------+---------+---------
 1  |    1 | 0       | 0       | 1
 1  |    2 | 0       | 1       | 0
 2  |    2 | 0       | 0       | 1
 2  |    3 | 0       | 1       | 0
 2  |    4 | 0       | 0       | 0 

how to realize it in MySQL 5.7

0

There are 0 best solutions below