How to set a local list/tuple variable in mysql

本文介绍了MySQL的FIND_IN_SET()函数,该函数用于判断字符串是否在由逗号分隔的字符串列表中,若存在则返回其位置。若搜索字符串不在列表中返回0,参数为NULL则返回NULL。还给出了相关语法及参考链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SET @list = 'a,b,c';
SELECT * FROM myTable WHERE FIND_IN_SET(myField, @list) > 0;

FIND_IN_SET(str,strlist)

Returns a value in the range of 1 to N if the string str is in the string list strlist consisting of N 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 type SET, the FIND_IN_SET() function is optimized to use bit arithmetic. Returns 0 if str is not in strlist or if strlist is the empty string. Returns NULL if either argument is NULL. 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

NameDescription
search stringA string which is to be looked for in following a list of arguments.
string listList of strings to be searched if they contain the search string.

Syntax Diagram:

MySQL FIND_IN_SET() Function - 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.

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值