Home  >  Article  >  Database  >  MySQL stored procedure-->long string splitting

long string splitting

-->

MySQL stored procedure-->long string splitting

黄舟
黄舟Original
2017-02-17 11:41:011254browse

MySQL stored procedure-->Long string split

DELIMITER $$ -- Delimiter
USE `test`$$ --
DROPPROCEDURE IF EXISTS `pro_split`$$ -- Delete if it already existsproc

CREATE PROCEDURE `test`.`pro_split` (varchar rids)
BEGIN
         DECLAREpo INT DEFAULT 0;  
-- 
定义一个位置点
         DECLARErid VARCHAR(20) DEFAULT "";   
-- 
定义一个
rid
变量
         DECLARErid_tmp VARCHAR(20) DEFAULT "";
         SET po= LOCATE(",",rids);  
-- 
找到第一个
,
的位置点
         SET rid=LEFT(po-1,rids);  
 -- 
截取出第一个
rid
         SETrid_tmp=SUBSTR(rids,po+1);   
-- 
设置剩下还没有处理的
rids
长字符串


WHILEpo >0 DO    
-- 
开始循环处理
rids
长字符串
                  INSERTINTO b_tmp SELECT pid,rid;  
--
把截取出来的
rid
放入临时表中
                  SET po =LOCATE(",",rid_tmp); 
 -- 
获取未处理中的长字符串的
,
的第一个位置点
                  SET rid=LEFT(po-1,rid_tmp);   
--
取出每一个循环中截取出来的
rid
                  SETrid_tmp=SUBSTR(rid_tmp,po+1);  
 -- 
重新设置未处理的长字符串。
         ENDWHILE  ;

END$$

DELIMITER;



PS : This is the default string splitting rule is ",". If you want to be more flexible, you can set an in parameter and substitute it when calling proc.

The above is the content of MySQL stored procedure--> long string splitting. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn