mysql split

CREATE DEFINER=`root`@`localhost` PROCEDURE `procedure_split`(IN `inputstring` varchar(1000), IN `delim` char(1))
BEGIN
    declare strlen int DEFAULT length(inputstring);
    declare last_index int DEFAULT 0;
    declare cur_index int DEFAULT 1;
    declare cur_char VARCHAR(200);
    declare len int;
    drop temporary table if exists splittable;
    create TEMPORARY table splittable(
        value VARCHAR(20)
    ) ;
    WHILE(cur_index<=strlen) DO    
    begin
        if substring(inputstring from cur_index for 1)=delim or cur_index=strlen then
            set len=cur_index-last_index-1;
            if cur_index=strlen then
               set len=len+1;
            end if;
            insert into splittable(`value`)values(substring(inputstring from (last_index+1) for len));
            set last_index=cur_index;
        end if;
        set cur_index=cur_index+1;
    END;
    end while;
    
    -- call PROCEDURE_split('中国,开源,社区',',');
	 -- select * from splittable;
END;


方法 二 :

CREATE DEFINER=`root`@`%` FUNCTION `func_get_split_string`(
f_string varchar(1000),f_delimiter varchar(5),f_order int) RETURNS varchar(255)  
BEGIN
  declare result varchar(255) default '';
  set result = reverse(substring_index(reverse(substring_index(f_string,f_delimiter,f_order)),f_delimiter,1));
  return result;
END



你可能感兴趣的:(mysql split)