这个函数是将存储的字段ID集合拆分开查询出名字,比如一张表存储了另一张表的ID集合是以"1,2,3,4"存储,调用此函数会显示对应的名字“值1,值2,值3,值4”
str:要拆分的字符串。
s:拆分分隔符。
CREATE DEFINER=`root`@`localhost` FUNCTION `const_function_strings`(str varchar(50),s varchar(10)) RETURNS varchar(50) CHARSET utf8
BEGIN
DECLARE strSub varchar(50) default '';
DECLARE startIndex int default 1;
DECLARE len int default 1;
while startIndex<=length(str) do
set len= LOCATE(s,str,startIndex)-startIndex;
if LOCATE(s,str,startIndex)=0 then
set len=length(str)-(startIndex-1);
set startIndex=length(str)+1;
end if;
set strSub=CONCAT(strSub,",",
(select TypeName from types
where
ID=substring(str,startIndex,len)));
if LOCATE(s,str,startIndex)>0 then
set startIndex=LOCATE(s,str,startIndex)+1;
end if;
end while;
set strSub=substring(strSub,2,length(strSub));
RETURN strSub;
END