mysql自定义函数实现统计一个字符串在另一个长字符串中出现的次数 (转载)

mysql自定义函数实现统计一个字符串在另一个长字符串中出现的次数

2014年09月23日 11:16:31 雪翊寒 阅读数 819

原文地址:https://blog.csdn.net/yixian918/article/details/39495777
  1. USE `test`;

  2. DROP function IF EXISTS `getSubNum`;

  3.  
  4.  
  5. DELIMITER $$

  6. USE `test`$$

  7. CREATE DEFINER=`root`@`localhost` FUNCTION `getSubNum`(str varchar(250),substr varchar(250)) RETURNS int(11)

  8. BEGIN

  9. declare num int default 0;

  10. declare indexStr int;

  11. set indexStr=INSTR(str, substr);

  12. while indexStr !=0 do

  13. set num = num + 1;

  14. set str=SUBSTRING(str,indexStr+LENGTH(substr)-1);

  15. set indexStr=INSTR(str, substr);

  16. end while;

  17. RETURN num;

  18. END$$

  19.  
  20.  
  21. DELIMITER ;

mysql自定义函数实现统计一个字符串在另一个长字符串中出现的次数 (转载)