MySQL FIND_IN_SET Function Explained

The FIND_IN_SET() function in MySQL is used to search for a specified value in a comma-separated string list and return its position in the list (starting from 1). The syntax of this function is as follows:

FIND_IN_SET(search_value, string_list)

search_value is the value to search for, and string_list is a comma-separated list of strings.

For example, if we have a string list ‘apple,banana,orange,grape’ and we want to find the position of the value ‘orange’ in the list, we can use the FIND_IN_SET() function.

SELECT FIND_IN_SET('orange', 'apple,banana,orange,grape');   -- 返回 3

In this example, the position of ‘orange’ in the string list ‘apple, banana, orange, grape’ is third. If the searched value is not in the list, return 0.

bannerAds