Home  >  Article  >  Database  >  What is the difference between the MySQL INSTR() and FIND_IN_SET() functions?

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

王林
王林forward
2023-09-22 10:25:051086browse

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)

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete