第一种:多次使用substring_index()的方法
DELIMITER $$
DROP PROCEDURE IF EXISTS `array`$$
CREATE PROCEDURE `array`()
BEGIN
SET @array_content="www mysql com hcymysql blog 51cto com";
SET @i=1;
SET @count=CHAR_LENGTH(@array_content)-CHAR_LENGTH(REPLACE(@array_content,‘ ‘,‘‘)) + 1;
-- 得出数组成员总数
CREATE TABLE test.tmp(field1 VARCHAR(100));
WHILE @i <= @count
DO
INSERT INTO test.tmp VALUES
(SUBSTRING_INDEX(SUBSTRING_INDEX(@array_content,‘ ‘,@i),‘ ‘,-1));
-- 依次插入每个成员
END WHILE;
END$$
DELIMITER ;
第二种:使用遍历每个字符的方法
drop procedure if exists Pro_DeleteLog; -- 存在即删除
create procedure Pro_DeleteLog( Qualification varchar(100)) -- 定义参数
begin
declare i int ;
declare Start1 int;
declare Length int ;
declare TotalLenght int ;
declare filed varchar(100) ;
declare sqlStr varchar(2000) ;
declare stmtNovelSearch varchar(2000) ;
set i=1;
set Start1=1;
set Length=0;
set TotalLenght=length(Qualification); -- 计算输入参数长度
select TotalLenght;
while i <=TotalLenght do -- i=1 开始
-- select SUBSTRING(Qualification,i,1);
if(SUBSTRING(Qualification,i,1)=‘,‘) -- 注意 mysql 的substring函数截取字符串是从1开始的,而不是0 与java JavaScript不同。
-- 从第i位开始截取一个字符,看是否等于,
then
set filed=SUBSTRING(Qualification,Start1,Length);-- 从start1开始截取length长度的字符
select filed;
set @sqlStr = CONCAT(‘insert into end_results(accessid,filedName,errinfo)select * from (select w1.accessid ,\‘‘,filed ,‘\‘ as filedName,‘,filed,‘ from wdd_audit w1 ,wdd_dbaccessinfo w2 where w1.accessid=w2.accessid )a where not exists(select e.‘,filed ,‘ from exp_results e where e.‘,filed,‘=a.‘,filed,‘ or(e.‘,filed,‘ is null and a.‘,filed,‘ is null) );‘);
-- 拼装sql语句
select @sqlStr;
PREPARE stmtNovelSearch FROM @sqlStr; -- prepare 必须采用这种传参方式
execute stmtNovelSearch;
deallocate prepare stmtNovelSearch;
set Start1=i+1;
set Length=0;
else
set Length=Length+1; -- 如果不是, 则截取的长度加1
end if;
set i=i+1;
end while;
end
-- 调用存储过程 字段名以,分隔 注意最后 , 结尾
call Pro_DeleteLog(‘payload,backresult,dataset,effectrow,clientuser,loginuser,clientprg,‘)
个人推荐第一种;
原文:http://www.cnblogs.com/caicaizi/p/4955519.html