Splitting mysql value into unknown number of parts

后端 未结 1 1073
清歌不尽
清歌不尽 2020-12-17 06:59

I have been given a mySQL database to restructure into an OpenCart installation.

I\'ve pulled most of the data across but in the old site \"product categories\" hav

相关标签:
1条回答
  • 2020-12-17 07:36

    One option that I recommend is to use common_schema and specifically functions get_num_tokens() and split_token(), this will help.

    Here a simple example of the use that you can adapt for your solution:

    /* CODE FOR DEMONSTRATION PURPOSES */
    
    /* Need to install common_schema - code.google.com/p/common-schema/ */ 
    
    /* Procedure structure for procedure `explode1` */     
    
    /*!50003 DROP PROCEDURE IF EXISTS  `explode1` */;
    
    DELIMITER $$
    
    CREATE PROCEDURE `explode1`(str varchar(65500), delim VARCHAR(255))
    BEGIN
        DECLARE _iteration, _num_tokens INT UNSIGNED DEFAULT 0;
        DROP TEMPORARY TABLE IF EXISTS `temp_explode`;
        CREATE TEMPORARY TABLE `temp_explode` (`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, `word` VARCHAR(200), PRIMARY KEY (`id`));
        SET _num_tokens := (SELECT `common_schema`.`get_num_tokens`(str, delim));
        WHILE _iteration < _num_tokens DO
            SET _iteration := _iteration + 1;
            INSERT INTO `temp_explode` (`word`) SELECT `common_schema`.`split_token`(str, delim, _iteration);
        END WHILE;
        SELECT `id`, `word` FROM `temp_explode`;
        DROP TEMPORARY TABLE IF EXISTS `temp_explode`;
    END $$
    
    DELIMITER ;
    
    /* TEST */
    CALL `explode1`('Lorem Ipsum is simply dummy text of the printing and typesetting', CHAR(32));
    
    0 讨论(0)
提交回复
热议问题