Convert JSON array in MySQL to rows

后端 未结 7 1416
无人共我
无人共我 2020-12-01 13:41

UPDATE: This is now possible in MySQL 8 via the JSON_TABLE function: https://dev.mysql.com/doc/refman/8.0/en/json-table-functions.html

I\'m loving t

7条回答
  •  北海茫月
    2020-12-01 14:17

    Here's how to do this with JSON_TABLE in MySQL 8+:

    SELECT *
         FROM
           JSON_TABLE(
             '[5, 6, 7]',
             "$[*]"
             COLUMNS(
               Value INT PATH "$"
             )
           ) data;
    

    You can also use this as a general string split function which MySQL otherwise lacks (similar to PG's regexp_split_to_table or MSSQL's STRING_SPLIT) by taking a delimited string and turning it into a JSON string:

    set @delimited = 'a,b,c';
    
    SELECT *
         FROM
           JSON_TABLE(
             CONCAT('["', REPLACE(@delimited, ',', '", "'), '"]'),
             "$[*]"
             COLUMNS(
               Value varchar(50) PATH "$"
             )
           ) data;
    

提交回复
热议问题