欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

mysql中find_in_set()函数的使用及in()用法详解

程序员文章站 2023-12-04 18:47:46
mysql手册中find_in_set函数的语法解释: find_in_set(str,strlist) str 要查询的字符串 strlist 字段名 参数以...

mysql手册中find_in_set函数的语法解释:

find_in_set(str,strlist)

str 要查询的字符串

strlist 字段名 参数以”,”分隔 如 (1,2,6,8,10,22)

查询字段(strlist)中包含(str)的结果,返回结果为null或记录

假如字符串str在由n个子链组成的字符串列表strlist 中,则返回值的范围在 1 到 n 之间。 一个字符串列表就是一个由一些被 ‘,' 符号分开的子链组成的字符串。如果第一个参数是一个常数字符串,而第二个是type set列,则find_in_set() 函数被优化,使用比特计算。 如果str不在strlist 或strlist 为空字符串,则返回值为 0 。如任意一个参数为null,则返回值为 null。这个函数在第一个参数包含一个逗号(‘,')时将无法正常运行。

看不懂概念也没事,按下面类子:

例子1:

select find_in_set('b', 'a,b,c,d');

结果:2

因为b 在strlist集合中放在2的位置 从1开始

select find_in_set('1', '1'); 返回 就是1 这时候的strlist集合有点特殊 只有一个字符串 其实就是要求前一个字符串 一定要在后一个字符串集合中才返回大于0的数

select find_in_set('2', '1,2'); 返回2
select find_in_set('6', '1'); 返回0 strlist中不存在str,所以返回0。

find_in_set()和in的区别:

弄个测试表来说明两者的区别

create table `tb_test` (
 `id` int(8) not null auto_increment,
 `name` varchar(255) not null,
 `list` varchar(255) not null,
 primary key (`id`)
);
insert into `tb_test` values (1, 'name', 'daodao,xiaohu,xiaoqin');
insert into `tb_test` values (2, 'name2', 'xiaohu,daodao,xiaoqin');
insert into `tb_test` values (3, 'name3', 'xiaoqin,daodao,xiaohu');

原来以为mysql可以进行这样的查询:

select id,name,list from tb_test where 'daodao' in(list); -- (一)

mysql中find_in_set()函数的使用及in()用法详解 

实际上这样是不行的, 这样只有当list字段的值等于'daodao'时(和in前面的字符串完全匹配),查询才有效,否则都得不到结果,即使'daodao'真的在list中。

再来看看这个:

select id,name,list from tb_test where 'daodao' in ('libk', 'zyfon', 'daodao'); -- (二)

mysql中find_in_set()函数的使用及in()用法详解 

这样是可以的。

这两条到底有什么区别呢?为什么第一条不能取得正确的结果,而第二条却能取得结果。原因其实是(一)中 (list) list是变量, 而(二)中 ('libk', 'zyfon', 'daodao')是常量。

所以如果要让(一)能正确工作,需要用

find_in_set():

select id,name,list from tb_test where find_in_set('daodao',list); -- (一)的改进版

mysql中find_in_set()函数的使用及in()用法详解 

总结:

所以如果list是常量,则可以直接用in, 否则要用find_in_set()函数。

也就是这两个sql是查询的效果是相同的:

select * from c_purchasingmasterdata where find_in_set(ekgrp,'c54,c02,c14,c60,c06,c61,c53,c51,c12,c08,c03,c07')
select * from c_purchasingmasterdata where ekgrp in ('c54','c02','c14','c60','c06','c61','c53','c51','c12','c08','c03','c07')

但是如果第二句sql里面的值是传入sql的一个变量字段,那么第二句sql就不好使了。要以实际情况决定用in还是用 find_in_set()函数 。

find_in_set()和like的区别:

主要的区别就是like是广泛的模糊查询,而 find_in_set() 是精确匹配,并且字段值之间用‘,'分开。

现在想查询拥有角色编号为2的用户,用like关键字查询:

select userid,username,userrole 角色 from `user` where userrole like '%2%';

结果:

mysql中find_in_set()函数的使用及in()用法详解 

用 find_in_set() 查询:

select userid,username,userrole 角色 from `user` where find_in_set('2',userrole)

结果:

mysql中find_in_set()函数的使用及in()用法详解 

显然用 find_in_set() 查询得到的结果才是我们想要的结果。所以他俩的

主要的区别就是like是广泛的模糊查询;而 find_in_set() 是精确匹配,并且字段值之间用‘,'分开,find_in_set查询的结果要小于like查询的结果。

mysql 中find_in_set()和in()用法比较

在mysql中in可以包括指定的数字,而find_in_set()用于特定的数据类型。

find_in_set 函数使用方法

个例子来说:

有个文章表里面有个type字段,它存储的是文章类型,有 1头条、2推荐、3热点、4图文...1,12,13 等等 。
现在有篇文章他既是 头条,又是热点,还是图文,
type中以 1,3,4 的格式存储。
那我们如何用sql查找所有type中有4图文标准的文章呢??
这就要我们的 find_in_set 出马的时候到了。

以下为引用的内容:

select * from article where find_in_set('4',type)

mysql手册中find_in_set函数的语法:

find_in_set(str,strlist)

假如字符串str 在由n 子链组成的字符串列表strlist 中,则返回值的范围在 1 到 n 之间。
一个字符串列表就是一个由一些被 ‘,' 符号分开的子链组成的字符串。如果第一个参数是一个常数字符串,而第二个是type set列,则 find_in_set() 函数被优化,使用比特计算。

如果str不在strlist 或strlist 为空字符串,则返回值为 0 。如任意一个参数为null,则返回值为 null。这个函数在第一个参数包含一个逗号(‘,')时将无法正常运行。

mysql> select find_in_set('b', 'a,b,c,d');

-> 2 因为b 在strlist集合中放在2的位置 从1开始

select find_in_set('1', '1'); 返回 就是1 这时候的strlist集合有点特殊 只有一个字符串 其实就是要求前一个字符串 一定要在后一个字符串集合中 才返回 大于0的数

select find_in_set('2', '1,2'); 返回2
select find_in_set('6', '1'); 返回0

注意:

select * from treenodes where find_in_set(id, '1,2,3,4,5');

使用find_in_set函数一次返回多条记录

id 是一个表的字段,然后每条记录分别是id等于1,2,3,4,5的时候
有点类似in (集合)

select * from treenodes where id in (1,2,3,4,5);

弄个测试表来说明两者的区别

create table `test` (
`id` int(8) not null auto_increment,
`name` varchar(255) not null,
`list` varchar(255) not null,
primary key (`id`)
)

insert into `test` values (1, 'name', 'daodao,www.jb51.net,xiaoqin');
insert into `test` values (2, 'name2', 'xiaohu,daodao,xiaoqin');
insert into `test` values (3, 'name3', 'xiaoqin,daodao,www.jb51.net');

原来以为mysql可以进行这样的查询:

select id, list, name from table where 'daodao' in (list);

(一)

实际上这样是不行的,这样只有当name是list中的第一个元素时,查询才有效,否则都得不到结果,即使'daodao'真的在list中。

再来看看这个:

select id, list, name from table where 'daodao' in ('libk', 'zyfon', 'daodao');

(二)

这样是可以的。
----------------------------------------------------------------
这两条到底有什么区别呢?为什么第一条不能取得正确的结果,而第二条却能取得结果。

原因其实是(一)中 (list) list是变量, 而(二)中 ('libk', 'zyfon', 'daodao')是常量。

所以如果要让(一)能正确工作,需要用find_in_set():

select id, list, name from table where find_in_set('daodao',list);

(一)的改进版。

总结:

所以如果list是常量,则可以直接用in, 否则要用find_in_set()函数。

以上所述是小编给大家介绍的mysql中find_in_set()函数的使用详解,希望对大家有所帮助