当前位置:网站首页>mysql——find_in_set用法
mysql——find_in_set用法
2022-06-11 22:55:00 【镇杰。】
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');
因为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); -- (一)
实际上这样是不行的, 这样只有当list字段的值等于’daodao’时(和IN前面的字符串完全匹配),查询才有效,否则都得不到结果,即使’daodao’真的在list中。
再来看看这个:
SELECT id,name,list from tb_test WHERE 'daodao' IN ('libk', 'zyfon', 'daodao'); -- (二)
这样是可以的。
这两条到底有什么区别呢?为什么第一条不能取得正确的结果,而第二条却能取得结果。原因其实是(一)中 (list) list是变量, 而(二)中 (‘libk’, ‘zyfon’, ‘daodao’)是常量。
所以如果要让(一)能正确工作,需要用
find_in_set():
SELECT id,name,list from tb_test WHERE FIND_IN_SET('daodao',list); -- (一)的改进版
总结:
所以如果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%';
结果:
用 find_in_set() 查询:
SELECT userid,username,userrole 角色 FROM `user` WHERE find_in_set('2',userrole)
结果:
显然用 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()函数的使用详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
边栏推荐
- [day6-7 intensive literature reading] a unifying Bayesian framework accounting for spatiotemporal interactions with a
- 【Day11-12 文献精读】On magnitudes in memory: An internal clock account of space-time interaction
- [day3 literature intensive reading] Oriental time and space interaction in tau and kappa effects
- 习题9-1 时间换算 (15 分)
- 基于模板配置的数据可视化平台
- Research Report on development trend and competitive strategy of global seabed leakage detection system industry
- 【自然语言处理】【多模态】ALBEF:基于动量蒸馏的视觉语言表示学习
- Matlab point cloud processing (XXIV): point cloud median filtering (pcmedian)
- 2022高压电工考试题模拟考试题库及在线模拟考试
- C# List. Can foreach temporarily / at any time terminate a loop?
猜你喜欢

Method for debugging wireless data packet capturing of Internet of things zigbee3.0 protocol e18-2g4u04b module

16 | floating point numbers and fixed-point numbers (Part 2): what is the use of a deep understanding of floating-point numbers?

【Day6-7 文献精读】A unifying Bayesian framework accounting for spatiotemporal interferences with a ...

Teacher lihongyi, NTU -- tips for DNN regulation

Meetup review how Devops & mlops solve the machine learning dilemma in enterprises?

【Day11-12 文献精读】On magnitudes in memory: An internal clock account of space-time interaction

【Day9 文献泛读】On the (a)symmetry between the perception of time and space in large-scale environments

Application of Lora wireless communication module Lora technology in smart home light control

Small program startup performance optimization practice

Is the product stronger or weaker, and is the price unchanged or reduced? Talk about domestic BMW X5
随机推荐
2022年起重机司机(限桥式起重机)考试题模拟考试题库及模拟考试
How to make scripts executable anywhere
SecurityContextHolder. getContext(). getAuthentication(). Getprincipal() gets username instead of userdetails
【自然语言处理】【多模态】ALBEF:基于动量蒸馏的视觉语言表示学习
C# List. Can foreach temporarily / at any time terminate a loop?
Meetup回顾|DevOps&MLOps如何在企业中解决机器学习困境?
postgresql10 进程
2022年安全员-A证考题模拟考试平台操作
Svn deploys servers and cleints locally and uses alicloud disks for automatic backup
动态规划之0-1背包问题(详解+分析+原码)
远程连接redis一会又断开重连
R7-1 sum of numeric elements of a list or tuple
Exercise 6-2 using functions to sum a special series of a numbers (20 points)
消息队列存储消息数据的 MySQL表
习题8-8 判断回文字符串 (20 分)
Postgresql10 process
习题9-6 按等级统计学生成绩 (20 分)
SDNU_ ACM_ ICPC_ 2022_ Weekly_ Practice_ 1st (supplementary question)
Research Report on development trend and competitive strategy of global seabed leakage detection system industry
【Day2 文献精读】Time in the mind: Using space to think about time