Splitting mysql value into unknown number of parts

别说谁变了你拦得住时间么 提交于 2019-11-29 05:16:55

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));
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!