Home > Database > Mysql Tutorial > How to use the LOCATE function in MySQL to find the position of a substring in a string

How to use the LOCATE function in MySQL to find the position of a substring in a string

王林
Release: 2023-07-25 09:45:20
Original
2606 people have browsed it

How to use the LOCATE function in MySQL to find the position of a substring in a string

In MySQL, there are many functions that can be used to process strings. Among them, the LOCATE function is a very useful function that can be used to find the position of a substring in a string.

The syntax of the LOCATE function is as follows:

LOCATE(substring, string, [position])
Copy after login

Among them, substring is the substring to be found, and string is the string to be found. The optional position parameter indicates the position to start the search, defaulting to 1.

Let’s look at a few examples of using the LOCATE function.

Example 1:

Suppose there is a string 'Hello, World!', and we hope to find the position of the comma.

SELECT LOCATE(',', 'Hello, World!');
Copy after login

Run the above code, the output result is 7, indicating the position of the comma in the string.

Example 2:

If we want to find the position of the character 'o' in the string 'Hello, World!' starting from position 12, we can use the position parameter.

SELECT LOCATE('o', 'Hello, World!', 12);
Copy after login

Run the above code, the output result is 13, indicating the position of the character 'o' in the string searched starting from position 12.

Example 3:

The LOCATE function can also be used in combination with other functions to achieve more complex string processing.

Suppose there is a table name_list that stores a set of names, separated by commas:

+--------------------------+
|         name_list         |
+--------------------------+
| John, Mary, Tom, Alice   |
+--------------------------+
Copy after login

Now we want to find the position of a name in name_list and put the results in comma-separated form return.

SELECT name_list,
       CONCAT(LOCATE('Alice', name_list),
              ',',
              IF(LOCATE('Alice', name_list), 1, 0),
              ',',
              IF(LOCATE('Alice', name_list), LOCATE(',', name_list, LOCATE('Alice', name_list)), 0)
              )
FROM name_list;
Copy after login

In the above code, the LOCATE function is used to find the position of 'Alice' in the name_list, and combined with the CONCAT function to return the results in comma-separated form. If 'Alice' does not exist, use the IF function to return 0.

Run the above code, the output result is:

+--------------------------+-----------------------------+
|         name_list         | 输出结果                    |
+--------------------------+-----------------------------+
| John, Mary, Tom, Alice   | 14,1,19                    |
+--------------------------+-----------------------------+
Copy after login

The above example details how to use the LOCATE function to find the position of a substring in a string in MySQL, and provides the corresponding code Example. By flexibly using the LOCATE function, we can easily perform string processing and query operations. It is hoped that readers can apply this knowledge in actual use and improve work efficiency.

The above is the detailed content of How to use the LOCATE function in MySQL to find the position of a substring in a string. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template