当前位置:网站首页>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 )
边栏推荐
- [PM2 details]
- ClickHouse(03)ClickHouse怎么安装和部署
- Eliminate the writing of 'if () else{}'
- 隐私计算助力数据的安全流通与共享
- 记一次使用Windbg分析内存“泄漏”的案例
- 瞅一瞅JUC提供的限流工具Semaphore
- Sophon CE Community Edition is online, and free get is a lightweight, easy-to-use, efficient and intelligent data analysis tool
- RPC协议详解
- How can cluster deployment solve the needs of massive video access and large concurrency?
- 《ClickHouse原理解析与应用实践》读书笔记(5)
猜你喜欢
Trust counts the number of occurrences of words in the file
U-Net: Convolutional Networks for Biomedical Images Segmentation
【HCIA-cloud】【1】云计算的定义、什么是云计算、云计算的架构与技术说明、华为云计算产品、华为内存DDR配置工具说明
Memory management chapter of Kobayashi coding
Fix vulnerability - mysql, ES
@Extension、@SPI注解原理
Find the first k small element select_ k
Insufficient picture data? I made a free image enhancement software
案例分享|金融业数据运营运维一体化建设
记一次使用Windbg分析内存“泄漏”的案例
随机推荐
rust统计文件中单词出现的次数
如何获取飞机穿过雷达两端的坐标
Fix vulnerability - mysql, ES
Leetcode notes: Weekly contest 300
sample_ What is the relationship between rate, sample and duration
@Extension、@SPI注解原理
[PM2 details]
Record eval() and no in pytoch_ grad()
The 10th global Cloud Computing Conference | Huayun data won the "special contribution award for the 10th anniversary of 2013-2022"
图片数据不够?我做了一个免费的图像增强软件
Numerical calculation method chapter8 Numerical solutions of ordinary differential equations
About Statistical Power(统计功效)
基于can总线的A2L文件解析(3)
buuctf-pwn write-ups (9)
《2022中国信创生态市场研究及选型评估报告》发布 华云数据入选信创IT基础设施主流厂商!
English sentence pattern reference
Find the first k small element select_ k
《ClickHouse原理解析与应用实践》读书笔记(5)
vs2017 qt的各种坑
Can communication of nano