当前位置:网站首页>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's darkhole_ two
- Copy the linked list with random pointer in the "Li Kou brush question plan"
- [PM2 details]
- Notes on common management commands of openshift
- JVM third talk -- JVM performance tuning practice and high-frequency interview question record
- About Estimation with Cross-Validation
- 记录Pytorch中的eval()和no_grad()
- Trust counts the number of occurrences of words in the file
- Sophon base 3.1 launched mlops function to provide wings for the operation of enterprise AI capabilities
- Nanjing University: Discussion on the training program of digital talents in the new era
猜你喜欢

Sophon kg upgrade 3.1: break down barriers between data and liberate enterprise productivity

图像分类,看我就够啦!

JVM third talk -- JVM performance tuning practice and high-frequency interview question record

Memory leak of viewpager + recyclerview

吴恩达团队2022机器学习课程,来啦
![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]

Privacy computing helps secure data circulation and sharing

Fix vulnerability - mysql, ES

瀚升优品app翰林优商系统开发功能介绍

Record eval() and no in pytoch_ grad()
随机推荐
About Statistical Power(统计功效)
【pm2详解】
使用JMeter录制脚本并调试
LeetCode 6111. Spiral matrix IV
英语句式参考
第十一届中国云计算标准和应用大会 | 华云数据成为全国信标委云计算标准工作组云迁移专题组副组长单位副组长单位
图片数据不够?我做了一个免费的图像增强软件
关于服装ERP,你想知道的都在这里了
Personal understanding of convolutional neural network
jdbc读大量数据导致内存溢出
U-Net: Convolutional Networks for Biomedical Images Segmentation
Introduction to Resampling
《力扣刷题计划》复制带随机指针的链表
Login and connect CDB and PDB
Electron installation problems
Isprs2022 / Cloud Detection: Cloud Detection with Boundary nets Boundary Networks Based Cloud Detection
websocket 工具的使用
Tupu software digital twin | visual management system based on BIM Technology
Le cours d'apprentissage de la machine 2022 de l'équipe Wunda arrive.
Record eval() and no in pytoch_ grad()