Convert JSON array in MySQL to rows

后端 未结 7 1429
无人共我
无人共我 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:30

    I was working in a report where there was a big json array list in one column. I modified the datamodel to store the relationship 1 to * instead of storing everything in one single column. For doing this process, I had to use a while in a stored procedure since I do not know the maximum size:

    DROP PROCEDURE IF EXISTS `test`;
    
    DELIMITER #
    
    CREATE PROCEDURE `test`()
    PROC_MAIN:BEGIN
    DECLARE numNotes int;
    DECLARE c int;
    DECLARE pos varchar(10);
    
    SET c = 0;
    SET numNotes = (SELECT 
    ROUND (   
            (
                LENGTH(debtor_master_notes)
                - LENGTH( REPLACE ( debtor_master_notes, "Id", "") ) 
            ) / LENGTH("Id")        
        ) AS countt FROM debtor_master
    order by countt desc Limit 1);
    
    DROP TEMPORARY TABLE IF EXISTS debtorTable;
    CREATE TEMPORARY TABLE debtorTable(debtor_master_id int(11), json longtext, note int);
    WHILE(c 

提交回复
热议问题