Find_in_set returns 空集?

Find_in_set returns an empty set?

我对 find_in_set 中的集合类型有一些疑问 这是代码:

create table set_test(id int,set_col SET('a','b','c','d'));
insert into set_test(id,set_col) values(1,'a,b'),(2,'a,b,b');
select * from set_test where find_in_set('a,b',set_col)

return空集!!! 为什么?

您不能使用 find_in_set 函数搜索其中包含逗号 'a,b' 的字符串,但只能搜索以逗号分隔的任何字符串,例如 a 或 [=15] =] 或 c,所以如果您尝试这样做将正常工作:

select * from set_test where find_in_set('a',set_col); 

但在您的情况下,您可以使用 like:

select * from set_test where set_col like '%a,b%';

函数FIND_IN_SET()的文档:

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.

它的第二个参数是一个字符串,其中包含以逗号分隔的值。您传递给 FIND_IN_SET() 的值(具有类型 SET 的列 set_col)与此描述相匹配。但是,它的第一个参数应该只是列表中包含的值之一。 a,b 不是作为第一个参数传递给 FIND_IN_SET() 的有效值,因为它包含以下值:ab。如果你想让它找到一些东西,你应该传递它 'a''b'

这也记录在段落的末尾,它说:

This function does not work properly if the first argument contains a comma (“,”) character.

find_in_set 函数 returns 搜索结果的数值,如果未找到则为 0。

你应该改变: select * 来自 set_test 其中 find_in_set('a,b',set_col);

收件人: select * 来自 set_test 其中 find_in_set('a,b',set_col) > 0;