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

前端 未结 10 697
萌比男神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:46

    In MySQL 5.6, by default JSON_EXTRACT is not available by default.
    If you still need to access json data in MySQL 5.6, you need to write custom function.

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

    This will help. I have created it and tested.

提交回复
热议问题