1. 程式人生 > 實用技巧 >mysql自定義函式實現統計一個字串在另一個長字串中出現的次數 (轉載)

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. DELIMITER $$

  4. USE `test`$$

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

  6. BEGIN

  7. declare num int default 0;

  8. declare indexStr int;

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

  10. while indexStr !=0 do

  11. set num = num + 1;

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

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

  14. end while;

  15. RETURN num;

  16. END$$

  17. DELIMITER ;