Mysql User-defined functions implement string matching _ MySQL

Source: Internet
Author: User
Mysql User-defined functions implement string matching bitsCN.com

Mysql User-defined functions implement string matching

First:

The first parameter of the fSearch function is a single string (that is, no special string is separated)

The first parameter of the fSearch function is a non-single string.

Multiple strings can also be matched.

Function code:

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 cIndex<=strNum10DO11set cStr=reverse(substring_index(reverse(substring_index(targetStr,',',cIndex)),',',1));12set flag=LOCATE(cStr,findStr);13if (flag>0&&cStr!='') THEN14RETURN flag;15END IF;16set cIndex=cIndex+1;17END WHILE;18RETURN 0;19END $$20DELIMITER ;

Use cases:

Add a field to the blog system to store the tag of the article. assume that the tag is 'Java, php, asp, web developer ', if you want to use this tag to find articles with similar tags, you can:

Select * from blog B where fSearch ('Java, php, asp, web developer', B. tags)> 0


BitsCN.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.