MySQL find string appears where

Source: Internet
Author: User

How to use locate and position functions in MySQL

    1. Find_in_set (Str,strlist)

      If the string str is in a string list strlist consisting of N substrings, the return value ranges from 1 to N. A list of strings is a string of self-chains that are separated by ', ' symbols. If the first argument is a constant string and the second is a type set column, the Find_in_set () function is optimized to use bit calculations. If STR is not strlist or strlist is an empty string, the return value is 0. If either parameter is NULL, the return value is null. This function will not work correctly when the first parameter contains a comma (', '). The return value is the position of STR in strlist, counting from 1.

    2. LOCATE (SUBSTR,STR)

      POSITION (substr in str)


      The function returns the position of the substring substr the first occurrence in the string str. If the substring substr does not exist in STR, the return value is 0.

    3. Str in (strlist)

Find the location where Str appears in Strlist. Returns False if it is not found. Strlist is a comma-concatenated string.
The difference between functions is that strlist in the first function find_in_set is a comma-concatenated string, typically a field in the database. Use this function when you need to find out if there is a value in a field.
The third function in () is just the opposite of the function find_in_set (), Strlist is a sequence of constant strings, and Str is a field in the database. The lookup field in the database is now in a sequence.
The second function is a string lookup function, similar to the Strpos () function in PHP, or the Charat () function in JavaScript. This function can accomplish the functions of the above two functions.

LOCATE (SUBSTR,STR)
POSITION (substr in str)
Returns the position of the substring substr the first occurrence in the string str . If the substring substr does not exist in STR, the return value is 0:
mysql> SELECT LOCATE (' Bar ', ' Foobarbar ');
-4 (description is counted starting from 1)
mysql> SELECT LOCATE (' Xbar ', ' foobar ');
0

This function is multi-byte safe. In MySQL 3.23, this function is case-sensitive, and when in MySQL 4.0, if either parameter is a binary string, it is case-sensitive.

LOCATE (Substr,str,pos)
Returns the position of the substring substr the first occurrence after the POS position in the string str. If SUBSTR does not return 0 in str:
mysql> SELECT LOCATE (' Bar ', ' Foobarbar ', 5);
7

This function is multi-byte safe. In MySQL 3.23, this function is case-sensitive, and when in MySQL 4.0, if either parameter is a binary string, it is case-sensitive.

A typical example is:
Slect * FROM TableName where LOCATE (colum1,colum1,1) >0

MySQL find string appears where

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.