mysql自定義函式實現統計一個字串在另一個長字串中出現的次數 (轉載)
阿新 • • 發佈:2020-10-21
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 ;