Home>Article>Database> How to use mysql locate()

How to use mysql locate()

青灯夜游
青灯夜游 Original
2022-06-15 15:00:47 9159browse

In mysql, locate() is mainly used for case-insensitive string search. It can return the position of the first occurrence of the substring in the string; the syntax is "LOCATE(substring to be searched) String, string[,start])", the parameter "start" is omitted and specifies the starting position of the search. The default value is 1. If the substring is not found in the original string, the locate() function returns 0.

How to use mysql locate()

The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.

MySQL LOCATE() function

#Usage: Case-insensitive string search and returns the first character in the string The position of the first occurrence of the substring

LOCATE() function can return the position of the first occurrence of the substring in the string; if the substring is not found in the original string, Then return 0.

Syntax:

LOCATE(substring, string [,start])
Parameters Description
substring Required. Substring tosearch instring
string Required. The string to search for
#start Optional. The starting position of the search. Position 1 is the default value

#If the specified substring is found in the original string, the return value must be greater than 0.

Usage examples of LOCATE() function

Example 1: Omit the start parameter

LOCATE(字符串1,字符串2)

Return string 1 in string 2 At the first occurrence, as long as string 2 contains string 1, the return value must be greater than 0.

Example 2: Do not omit the start parameter

LOCATE(字符串1,字符串2,start)

Returns the position where string 1 first appears in string 2, starting from the position start Start;

  • If 0 is returned, it means there is no more

    ## after starting from the start position
  • If the return value is greater than 0, it means

[Related recommendations:

mysql video tutorial]

The above is the detailed content of How to use mysql locate(). For more information, please follow other related articles on the PHP Chinese website!

Statement:
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