Home > Database > Mysql Tutorial > body text

What is the difference between the MySQL INSTR() and FIND_IN_SET() functions?

王林
Release: 2023-09-22 10:25:05
forward
1091 people have browsed it

MySQL INSTR() 和 FIND_IN_SET() 函数有什么区别?

As we all know, both functions are used to search strings based on the arguments they provide, but there are some significant differences between them as shown below

  • The string list used by the FIND_IN_SET() function is itself a string containing comma-separated substrings. Whereas the INSTR() function contains a string from which it will find the first occurrence of the substring, if it exists.
  • For integers, FIND_IN_SET() is more suitable than the INSTR() function. It can be understood through the following example

Example

mysql> Select IF(INSTR('10,11,12,13',2) > 0,1,0) As Result;
+--------+
| Result |
+--------+
|      1 |
+--------+
1 row in set (0.05 sec)

mysql> Select IF(FIND_IN_SET(2,'10,11,12,13') > 0,1,0)As Result;
+--------+
| Result |
+--------+
|      0 |
+--------+
1 row in set (0.00 sec)
Copy after login

From the result set of the above example, we can see that the INSTR() function returns 1 or even 2 because the character does not exist in the parameter string. But the FIND_IN_SET() function returns 0, which is the correct answer.

The above is the detailed content of What is the difference between the MySQL INSTR() and FIND_IN_SET() functions?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
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