mysql LOCATE any
时间: 2023-10-22 17:07:44 浏览: 98
`LOCATE` is a MySQL function that finds the position of a substring within a string. The syntax for `LOCATE` is as follows:
```
LOCATE(substr, str)
```
where `substr` is the substring to search for, and `str` is the string to search within.
To search for any one of several substrings, you can use the `REGEXP` operator and provide a regular expression pattern that matches any of the substrings. For example:
```
SELECT LOCATE(REGEXP_REPLACE('foo|bar|baz', '\\|', '|'), 'foobarbaz');
```
This will search for the substrings "foo", "bar", or "baz" within the string "foobarbaz", and return the position of the first occurrence of any of them. The `REGEXP_REPLACE` function is used here to replace the pipe symbol (`|`) with a regular expression alternation (`|`) so that the resulting regular expression matches any of the substrings.
阅读全文