Home > Database > Mysql Tutorial > mysql自定义函数实现字符串匹配_MySQL

mysql自定义函数实现字符串匹配_MySQL

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-01 13:30:20
Original
1051 people have browsed it

bitsCN.com

mysql自定义函数实现字符串匹配
 

先来一个截图:

fSearch函数的第一个参数为单一字符串(即 没有特殊字符串隔开)

mysql自定义函数实现字符串匹配_MySQL

fSearch函数的第一个参数非单一字符串

mysql自定义函数实现字符串匹配_MySQL

多个字符串同样可以匹配。

 

函数代码:

01DELIMITER $$02Create   function fSearch(targetStr VARCHAR(100),findStr VARCHAR(100)) RETURNS INT03BEGIN04DECLARE strNum INT;05DECLARE cIndex INT DEFAULT 1;06DECLARE cStr VARCHAR(50);07DECLARE flag INT;08set strNum=1+(length(targetStr) - length(replace(targetStr,&#39;,&#39;,&#39;&#39;)));09WHILE cIndex<=strNum10DO11set cStr=reverse(substring_index(reverse(substring_index(targetStr,&#39;,&#39;,cIndex)),&#39;,&#39;,1));12set flag=LOCATE(cStr,findStr);13if (flag>0&&cStr!=&#39;&#39;) THEN14RETURN flag;15END IF;16set cIndex=cIndex+1;17END WHILE;18RETURN 0;19END $$20DELIMITER ;
Copy after login

使用场景:

加入博客系统中有个字段 存储文章所属的标签,假设标签为‘java,php,asp,web开发’ ,此时如果想通过该标签找到具有同类标签的文章 则可:

select *from blog b where fSearch(&#39;java,php,asp,web开发&#39;,b.tags)>0
Copy after login

 


bitsCN.com
Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template