您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

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

2024/5/19 18:15:52发布65次查看
bitscn.com
mysql自定义函数实现字符串匹配
先来一个截图:
fsearch函数的第一个参数为单一字符串(即 没有特殊字符串隔开)
fsearch函数的第一个参数非单一字符串
多个字符串同样可以匹配。
函数代码:
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,',','')));09while cindex0&&cstr!='') then14return flag;15end if;16set cindex=cindex+1;17end while;18return 0;19end $$20delimiter ;
使用场景:
加入博客系统中有个字段 存储文章所属的标签,假设标签为‘java,php,asp,web开发’ ,此时如果想通过该标签找到具有同类标签的文章 则可:
select *from blog b where fsearch('java,php,asp,web开发',b.tags)>0
bitscn.com
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product