简体   繁体   中英

select distinct values from json arrays in mysql database

I have table like this my_table

id    my_json
1     ['1','2','3']
2     ['2']
3     ['2','3']
...
12000 ....

I want to find all distinct values in json's arrays like this result '1' '2' '3'

I have this code but i need split values to rows

set @items = (SELECT 
     GROUP_CONCAT(
        REPLACE(REPLACE(lower(my_json), ']', ''), '[', '')
        SEPARATOR ','
    )
FROM my_table);
SELECT CONCAT ('[',@items,']') AS jarray;

result is
[1,2,3]

probably somebody have ideas?

You can use json_table :

select json_arrayagg(t2.v) from (select distinct t1.v from tbl t 
  cross join json_table(t.my_json, '$[*]' columns( v int path '$')) t1) t2

See fiddle .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM