mysql find_in_set函数

–FIND_IN_SET(子集,合集(要么等于子集,要么子集在合集逗号分隔部分里面))

已知 bind_module 是字符串,多个存储例如: 1,2,3,4,5,6,7,8,9 ,也是一个空字符串。

select * from student1 where FIND_IN_SET("1",`bind_module`)

此时,如果 bind_module=1 ,或者该集合里包含 1,条件就匹配。

如果要取反,可以在 FIND_IN_SET 前取反,使用 ! 符号,例如 :

select * from student1 where !FIND_IN_SET("1",`bind_module`)

FILD_IN_SET 和 IN 的一些用法:

已知一个查询,返回字段 $str = ‘1,2,3,4’

则要查询另一个表id为1 或 2 或 3 或 4的数据。

此时不能使用 where id in($str),因为如果使用 in 则会强制转为int,也就是相当于 where id in(1)

in 时一般是单列,可以多行,例如:

column
1
2
3
4
5

如果返回的是 ‘1,2,3,4,5’ 这样的字符串格式,则明显不能用 in ,怎么办呢?用以下sql:

where FILD_IN_SET(`id`, '1,2,3,4,5');

也就是说:where id in(1,2,3,4,5) 相当于 where FILD_IN_SET(id, ‘1,2,3,4,5’) 。

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注