当前位置:网站首页>MYSQL中 find_in_set() 函数用法详解
MYSQL中 find_in_set() 函数用法详解
2022-07-05 18:21:00 【fastjson_】
1、官方涵义(MySQL手册中语法说明)
FIND_IN_SET(str,strlist),该函数的作用是查询字段(strlist) 中是否包含(str)的结果,返回结果为 null或记录 。
str 要查询的字符串
strlist 需查询的字段,参数以”,”分隔,形式如 (1,2,6,8,10,22)
假如字符串str在由N个子链组成的字符串列表strlist 中,则返回值的范围在 1 到 N 之间。 一个字符串列表就是一个由一些被 ‘,’ 符号分开的子链组成的字符串。
如果第一个参数是一个常数字符串,而第二个是type SET列,则FIND_IN_SET() 函数被优化,使用比特计算。 如果str不在strlist 或strlist 为空字符串,则返回值为 0 。如任意一个参数为NULL,则返回值为 NULL。这个函数在第一个参数包含一个逗号( , )时将无法正常运行。
示例:
SELECT FIND_IN_SET('b', 'a,b,c,d');
// 结果:2
// 因为 b 在strlist集合中2的位置, a是位置1
select FIND_IN_SET('1', '1');
// 结果:1
// 这时候的strlist集合有点特殊,只有一个字符串
select FIND_IN_SET('2', '1,2');
// 结果:2
select FIND_IN_SET('6', '1');
// 结果:0 strlist中不存在str,所以返回0。综上: FIND_IN_SET函数中,若前一个字符串包含在后一个字符串集合中,返回大于0的数,该数为前一个字符串在后一个字符串中的位置。
2、find_in_set() 和 in 的区别
新建测试表,增加几条测试数据。
CREATE TABLE `test` (
`ID` int(11) NOT NULL,
`LIST` varchar(255) DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of test
-- ----------------------------
INSERT INTO `test` VALUES ('1', 'AA,BB,CC');
INSERT INTO `test` VALUES ('2', 'AA,BB');
INSERT INTO `test` VALUES ('3', 'AA');find_in_set()和in的查询比较
-- IN查询字段条件
SELECT id,list,'字段条件' AS 'IN字段' from TEST WHERE list in ('AA');
-- IN查询常量条件-条件为真
SELECT id,list,'条件为真' AS 'IN常量条件为真' from TEST WHERE 'AA' in ('AA','BB');
-- IN查询常量条件-条件为假
SELECT id,list,'条件为假' AS 'IN常量条件为假' from TEST WHERE 'AA' in ('BB','CC');
-- FIND_IN_SET字段条件
SELECT id,list,'字段条件' AS 'FIND_IN_SET字段' from TEST WHERE FIND_IN_SET('AA', list);
-- FIND_IN_SET常量条件为真
SELECT id,list,'条件为真' AS 'FIND_IN_SET常量条件为真' from TEST WHERE FIND_IN_SET('AA', 'AA,BB,CC');
-- FIND_IN_SET常量条件为假
SELECT id,list,'条件为假' AS 'FIND_IN_SET常量条件为假' from TEST WHERE FIND_IN_SET('AA', 'BB,CC');
区别:
1、in后面只能跟常量, find_in_set()函数可以使用常量或字段。
2、in是完全匹配,find_in_set()函数是精确匹配,字段值以英文”,”分隔。
另:like是广泛的模糊匹配,字符串中没有分隔符,Find_IN_SET 是精确匹配,字段值以英文”,”分隔,Find_IN_SET查询的结果要小于like查询的结果。
3、应用场景
1、文章表type字段查询
文章表里面有个type字段,它存储的是文章类型,有 1头条、2推荐、3热点、4图文等等 。现在有篇文章他既是头条,又是热点,还是图文,type中以 1,3,4 的格式存储。那我们如何用sql查找所有type中有4的图文类型的文章呢?
select * from article where FIND_IN_SET('4',type)2、部门树查询,匹配当前节点及所有子节点
数据表字段说明

匹配部门id或父id为100的数据
SELECT dept_id FROM sys_dept WHERE dept_id = 100 or FIND_IN_SET( 100 , ancestors ) 
边栏推荐
- 含重复元素取不重复子集[如何取子集?如何去重?]
- vulnhub之darkhole_2
- Record eval() and no in pytoch_ grad()
- 【pm2详解】
- [paddleclas] common commands
- 《ClickHouse原理解析与应用实践》读书笔记(5)
- Use of print function in MATLAB
- How to solve the error "press any to exit" when deploying multiple easycvr on one server?
- 吳恩達團隊2022機器學習課程,來啦
- About statistical power
猜你喜欢

FCN: Fully Convolutional Networks for Semantic Segmentation

Star Ring Technology launched transwarp Navier, a data element circulation platform, to help enterprises achieve secure data circulation and collaboration under privacy protection

LeetCode 6109. 知道秘密的人数
![Maximum artificial island [how to make all nodes of a connected component record the total number of nodes? + number the connected component]](/img/8b/a60fc36115580f018445e4c2a28a9d.png)
Maximum artificial island [how to make all nodes of a connected component record the total number of nodes? + number the connected component]

第十一届中国云计算标准和应用大会 | 云计算国家标准及白皮书系列发布 华云数据全面参与编制

Record eval() and no in pytoch_ grad()

The 11th China cloud computing standards and Applications Conference | China cloud data has become the deputy leader unit of the cloud migration special group of the cloud computing standards working

华夏基金:基金行业数字化转型实践成果分享
![最大人工岛[如何让一个连通分量的所有节点都记录总节点数?+给连通分量编号]](/img/8b/a60fc36115580f018445e4c2a28a9d.png)
最大人工岛[如何让一个连通分量的所有节点都记录总节点数?+给连通分量编号]

Fix vulnerability - mysql, ES
随机推荐
Record eval() and no in pytoch_ grad()
Gimp 2.10 tutorial "suggestions collection"
彻底理解为什么网络 I/O 会被阻塞?
Is it safe for Apple mobile phone to speculate in stocks? Is it a fraud to get new debts?
Sophon base 3.1 launched mlops function to provide wings for the operation of enterprise AI capabilities
The easycvr platform reports an error "ID cannot be empty" through the interface editing channel. What is the reason?
Le cours d'apprentissage de la machine 2022 de l'équipe Wunda arrive.
node_exporter内存使用率不显示
【PaddleClas】常用命令
[QNX hypervisor 2.2 user manual]6.3.2 configuring VM
How to improve the thermal management in PCB design with the effective placement of thermal through holes?
Generate classes from XML schema
瞅一瞅JUC提供的限流工具Semaphore
Is it safe to open an account and register stocks for stock speculation? Is there any risk? Is it reliable?
How can cluster deployment solve the needs of massive video access and large concurrency?
New words new words new words new words [2]
图像分类,看我就够啦!
苹果手机炒股安全吗?打新债是骗局吗?
Logical words in Articles
LeetCode 6109. Number of people who know the secret