我已創建1功能和1是從MySQL網站找到的:
該函數給出了定義分隔符的字符串中給定位置的值。
DELIMITER $$
CREATE FUNCTION `SplitString`(x varchar(255), delim varchar(12), pos int) RETURNS varchar(255) CHARSET latin1
return replace(substring(substring_index(x, delim, pos), length(substring_index(x, delim, pos - 1)) + 1), delim, '')
該函數使用上述函數並服務於我的目的。
DELIMITER $$
CREATE FUNCTION `get_matched`(my_num varchar(100), to_match_num VARCHAR(100)) RETURNS int(11)
BEGIN
DECLARE temp, total INT;
SET total=0;
SET temp = IF(FIND_IN_SET(SplitString(my_num,',',1), to_match_num)>0,1,0);
SET total = total+ temp;
SET temp = IF(FIND_IN_SET(SplitString(my_num,',',2), to_match_num)>0,1,0);
SET total = total+ temp;
SET temp = IF(FIND_IN_SET(SplitString(my_num,',',3), to_match_num)>0,1,0);
SET total = total+ temp;
SET temp = IF(FIND_IN_SET(SplitString(my_num,',',4), to_match_num)>0,1,0);
SET total = total+ temp;
SET temp = IF(FIND_IN_SET(SplitString(my_num,',',5), to_match_num)>0,1,0);
SET total = total+ temp;
SET temp = IF(FIND_IN_SET(SplitString(my_num,',',6), to_match_num)>0,1,0);
SET total = total+temp;
RETURN total;
END