How to get values from MySQL(5.6) column if that contains json document as string

前端 未结 10 695
萌比男神i
萌比男神i 2020-12-02 18:48

How to get values from MySQL(5.6) column if that contains JSON document as a string

For example, if we have a table - employee in that we have three columns id, nam

10条回答
  •  隐瞒了意图╮
    2020-12-02 19:36

    Rahul's answer did not work quite well for me, so I edited it and this worked for me:

    DELIMITER $$
    
    DROP FUNCTION IF EXISTS `json_extract_c`$$
    
    CREATE FUNCTION `json_extract_c`(
    details TEXT,
    required_field VARCHAR (255)
    ) RETURNS TEXT CHARSET latin1
    BEGIN
    SET details = TRIM(LEADING '{' FROM TRIM(details));
    SET details = TRIM(TRAILING '}' FROM TRIM(details));
    RETURN TRIM(
        BOTH '"' FROM SUBSTRING_INDEX(
            SUBSTRING_INDEX(
                SUBSTRING_INDEX(
                    details,
                    CONCAT(
                        '"',
                        SUBSTRING_INDEX(required_field,'$.', - 1),
                        '":'
                    ),
                    - 1
                ),
                ',"',
                1
            ),
            ':',
            -1
        )
    ) ;
    END$$
    
    DELIMITER ;
    

提交回复
热议问题