此函数只能用于判断字段值长度为17的,用逗号分隔开的字符串集合,用于判断setB集合中是否全部包含了setA,相等也返回1,不包含返回0
CREATE DEFINER=`root`@`%` FUNCTION `IS_ALL_INCLUDE`(setA longtext,setB longtext) RETURNS longtext CHARSET utf8mb4
BEGIN
DECLARE idx INT DEFAULT 0 ;
DECLARE idxA INT DEFAULT 0 ;
DECLARE len INT DEFAULT 0;
DECLARE lenA INT DEFAULT 0;
DECLARE clen INT DEFAULT 0;
DECLARE clenA INT DEFAULT 0;
DECLARE iscontain INT DEFAULT 0;
DECLARE tmpStr longtext;
DECLARE tmpStrA longtext;
DECLARE curt longtext;
DECLARE curtA longtext;
IF setA = setB THEN RETURN 1;
END IF;
SET lenA = CAST((char_length(setA)+1)/18 AS SIGNED);
WHILE idxA <= lenA DO
SET idxA = idxA + 1;
SET tmpStrA = SUBSTRING_INDEX(setA,",",idxA);
SET clenA = char_length(tmpStrA);
IF idxA = 1 THEN SET curtA = tmpStrA;
ELSE SET curtA = SUBSTRING(setA,(idxA-1)*18+1,17);
END IF;
SET idx = 0;
SET len = 0;
SET clen = 0;
SET len = CAST((char_length(setB)+1)/18 AS SIGNED);
WHILE idx <= len DO
SET idx = idx + 1;
SET tmpStr = SUBSTRING_INDEX(setB,",",idx);
IF idx = 1 THEN SET curt = tmpStr;
ELSE SET curt = SUBSTRING(setB,(idx-1)*18+1,17);
END IF;
#IF idxA=2 AND idx = 3 THEN RETURN curt;
#END IF;
IF curtA <=> curt THEN SET iscontain = iscontain+1;
END IF;
END WHILE;
END WHILE;
IF iscontain != idxA THEN RETURN 0;
END IF;
RETURN 1;
END
优化后滴
CREATE DEFINER=`root`@`%` FUNCTION `IS_ALL_CONTAIN`(setA longtext,setB longtext) RETURNS longtext CHARSET utf8mb4
BEGIN
DECLARE dhNum INT DEFAULT 0 ;
DECLARE dhNumA INT DEFAULT 0 ;
DECLARE dhNumB INT DEFAULT 0 ;
DECLARE dhNumC INT DEFAULT 0 ;
DECLARE tmpStr longtext;
DECLARE tmpStrA longtext;
DECLARE tmpStrB longtext;
DECLARE tmpStrC longtext;
DECLARE result longtext;
IF setA = setB THEN RETURN 1;
END IF;
IF (locate(",",setB) = 0 and locate(setB,setA)=0) THEN RETURN 0;
END IF;
set dhNum = (LENGTH(setB)-LENGTH(REPLACE(setB,",","")))/LENGTH(",");
set dhNumC = (LENGTH(setA)-LENGTH(REPLACE(setA,",","")))/LENGTH(",");
IF (dhNum>dhNumC) THEN RETURN 0;
END IF;
set dhNumB = ROUND(dhNum);
set tmpStrC = setB;
#循环setB
WHILE dhNumA <= dhNumB DO
if (dhNumA<dhNumB)
THEN
SET dhNumA = dhNumA + 1;
SET tmpStr = substring_index(tmpStrC,",",1);
SET tmpStrC = REPLACE (tmpStrC, CONCAT(tmpStr, ','), "");
ELSE
SET dhNumA = dhNumA + 1;
SET tmpStr = tmpStrC;
END IF;
#RETURN CONCAT("peoductId:",tmpStr,",peoductIdList:",setA);
if (locate(tmpStr,setA)=0) THEN RETURN 0;
END IF;
END WHILE;
#if(LOCATE(tmpStr,setA)=0) THEN RETURN 0;
#END IF;
RETURN 1;
END