As a result of today's sub-classification of a site for the SQL InStr () and locate () string operation function, below to make a note down, the need for friends can refer to.
INSTR (STR,SUBSTR)
Returns the first occurrence of a substring of string str. This is the same as the two-parameter form of locate (), unless the order of the arguments is reversed.
The code is as follows |
Copy Code |
mysql> SELECT INSTR (' Foobarbar ', ' Bar '); 4 mysql> SELECT INSTR (' Xbar ', ' foobar '); 0 |
LOCATE (SUBSTR,STR), LOCATE (Substr,str,pos)
The first syntax returns the first occurrence of the string str neutron string substr. The second syntax returns the first occurrence of the string str neutron string substr, starting at Pos. If SUBSTR is not in Str, the return value is 0.
The code is as follows |
Copy Code |
mysql> SELECT LOCATE (' Bar ', ' Foobarbar '); 4 mysql> SELECT LOCATE (' Xbar ', ' foobar '); 0 mysql> SELECT LOCATE (' Bar ', ' Foobarbar ', 5); 7
|
This function supports multibyte characters and is case-sensitive only if at least one parameter is a binary string.
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:
The code is as follows |
Copy Code |
mysql> SELECT POSITION (' Bar ', ' Foobarbar '); 4 mysql> SELECT POSITION (' Xbar ', ' foobar '); 0 |
Efficiency test
The code is as follows |
Copy Code |
SELECT * from ' O_soft ' WHERE LOCATE (' d200 ', Tid2) >0 |
The query results returned by MySQL are empty (that is, 0 rows). (Query takes 0.0050 seconds)
The code is as follows |
Copy Code |
SELECT * from ' O_soft ' WHERE INSTR (' d200 ', Tid2) >0 |
The query results returned by MySQL are empty (that is, 0 rows). (Query takes 0.0009 seconds)
SQL InStr () and locate () string lookup functions