mysql自定义函数实现统计一个字符串在另一个长字符串中出现的次数 (转载)
mysql自定义函数实现统计一个字符串在另一个长字符串中出现的次数
2014年09月23日 11:16:31 雪翊寒 阅读数 819
原文地址:https://blog.csdn.net/yixian918/article/details/39495777
-
USE `test`;
-
DROP function IF EXISTS `getSubNum`;
-
DELIMITER $$
-
USE `test`$$
-
CREATE DEFINER=`root`@`localhost` FUNCTION `getSubNum`(str varchar(250),substr varchar(250)) RETURNS int(11)
-
BEGIN
-
declare num int default 0;
-
declare indexStr int;
-
set indexStr=INSTR(str, substr);
-
while indexStr !=0 do
-
set num = num + 1;
-
set str=SUBSTRING(str,indexStr+LENGTH(substr)-1);
-
set indexStr=INSTR(str, substr);
-
end while;
-
RETURN num;
-
END$$
-
DELIMITER ;