SET @list = 'a,b,c';
SELECT * FROM myTable WHERE FIND_IN_SET(myField, @list) > 0;
Returns a value in the range of 1 to
N
if the stringstr
is in the string liststrlist
consisting ofN
substrings. A string list is a string composed of substrings separated by,
characters. If the first argument is a constant string and the second is a column of typeSET
, theFIND_IN_SET()
function is optimized to use bit arithmetic. Returns0
ifstr
is not instrlist
or ifstrlist
is the empty string. ReturnsNULL
if either argument isNULL
. This function does not work properly if the first argument contains a comma (,
) character.
mysql> SELECT FIND_IN_SET('b','a,b,c,d'); -> 2
FIND_IN_SET() function
MySQL FIND_IN_SET() returns the position of a string if it is present (as a substring) within a list of strings. The string list itself is a string contains substrings separated by ‘,’ (comma) character.
This function returns 0 when search string does not exist in the string list and returns NULL if either of the arguments is NULL.
Syntax:
FIND_IN_SET (search string, string list)
Arguments
Name | Description |
---|---|
search string | A string which is to be looked for in following a list of arguments. |
string list | List of strings to be searched if they contain the search string. |
Syntax Diagram:
https://stackoverflow.com/questions/53231415/how-to-set-a-local-list-tuple-variable-in-mysql
https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_find-in-set
https://www.w3resource.com/mysql/string-functions/mysql-find_in_set-function.php#:~:text=GMT%20%2B8%20hours)-,FIND_IN_SET()%20function,within%20a%20list%20of%20strings.&text=This%20function%20returns%200%20when,of%20the%20arguments%20is%20NULL.