Difference between LOCATE() and FIND_IN_SET() Function
When using the LOCATE() function for integers, suppose we need 1 to return from LOCATE() , if the integer 3 is in the set 1,2,3,4,5,.. , the following MySQL commands can be written:
mysql> SELECT IF(LOCATE(3,'1,2,3,4,5,6,7,8,9')>0,1,0); +-----------------------------------------+ | IF(LOCATE(3,'1,2,3,4,5,6,7,8,9')>0,1,0) | +-----------------------------------------+ | 1 | +-----------------------------------------+ 1 row in set (0.06 sec)
The above command works correctly because the set contains the number 3, but if we write the following commands, look what happened
mysql> SELECT IF(LOCATE(3,'11,12,13,14,15')>0,1,0); +--------------------------------------+ | IF(LOCATE(3,'11,12,13,14,15')>0,1,0) | +--------------------------------------+ | 1 | +--------------------------------------+ 1 row in set (0.02 sec)
Above 3 is not present as the number three (3) in the given set, although LOCATE() returns 1. To avoid this situation, you can use the FIND_IN_SET() function. The following is an example:
mysql> SELECT IF(FIND_IN_SET(3,'11,12,13,4,5,6,7,8,9')>0,1,0); +-------------------------------------------------+ | IF(FIND_IN_SET(3,'11,12,13,4,5,6,7,8,9')>0,1,0) | +-------------------------------------------------+ | 0 | +-------------------------------------------------+ 1 row in set (0.05 sec)
So, the LOCATE () function is very suitable for a string, but not suitable for an integer.
You can find examples, loans and additional information here.
So, in your example FIND_IN_SET return 0 , because the specified set does not have 3 , but LOCATE() returns 1 , it treats the given set as a string, but not a comma-separated value, and 3 present in number 13