MySQL uses built-in functions for fuzzy search (locate (), etc.)

Source: Internet
Author: User

Commonly used in a total of 4 methods, as follows: 1. Using the Locate () method

1.1. General Usage:

SELECT ' column ' from ' Table ' where locate ('keyword', ' condition ') >0

Java-like indexOf (); however locate () as long as the found returned results are greater than 0 (even if the content of the query is the first part), it is not found to return 0;

1.2. Specify the location:

SELECT LOCATE (' Bar ', ' Foobarbar ', 5); -7 (starting from the fifth position in Foobarbar)

2. Use the InStr () function (which is said to be an alias function of locate ())

SELECT ' column ' from ' Table ' where InStr (' condition ', 'keyword') >0

The only difference is that the location of the query content is different, see the keyword in the SQL statement

3. Using the position () method, (also said to be the alias function of the Locate () method, the same function)

SELECT ' column ' from ' table ' where position ('keyword' in ' condition ')

But it is no longer judged by the return value, but by using the keyword in

4. Using the Find_in_set () function

such as: find_in_set (str,strlist),strlist must be a comma-delimited string

If the string str is a string list of n substrings in the Strlist, the range of return values is 1 to n

SELECT Find_in_set (' B ', 'a,b,c,d');  SELECT find_in_set (' B ', ' a,b,c,d ')                       2                                                      

Summary : The difference between locate, position, and InStr is just the same as the parameter location, and the parameter of locate more than one starting position.

Find_in_set () is a very special existence, but they all return the position of the substring to find in the specified string.

the speed of the first 3 is a little faster than with like. (It's a pity that none of the four functions can use an index.)

MySQL uses built-in functions for fuzzy search (locate (), etc.)

Related Article

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.