惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
Martin Fowler
Martin Fowler
B
Blog RSS Feed
D
DataBreaches.Net
L
LangChain Blog
月光博客
月光博客
S
SegmentFault 最新的问题
阮一峰的网络日志
阮一峰的网络日志
V
Visual Studio Blog
美团技术团队
Jina AI
Jina AI
博客园 - 司徒正美
雷峰网
雷峰网
Last Week in AI
Last Week in AI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
IT之家
IT之家
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
小众软件
小众软件
罗磊的独立博客
博客园_首页
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
A
About on SuperTechFans
Engineering at Meta
Engineering at Meta

Hsu Yeung 的博客

三圣花市 | Hsu Yeung 的博客 网站支持 Live Photo 图片展示 | Hsu Yeung 的博客 梦 | Hsu Yeung 的博客 周末午餐 | Hsu Yeung 的博客 张家界 | Hsu Yeung 的博客 话剧《寻她芳踪·张爱玲》 | Hsu Yeung 的博客 博客自动生成文章目录 | Hsu Yeung 的博客 灭螂行动! | Hsu Yeung 的博客 张靓颖成都演唱会 | Hsu Yeung 的博客 最近对博客做的一些微调总结 | Hsu Yeung 的博客 Windows 上安装 Lua | Hsu Yeung 的博客 网站支持展示 B 站 iframe 视频 SpringBoot 中使用 RedisTemplate 存取整数值的坑 | Hsu Yeung 的博客 SQL JOIN 中 ON 与 WHERE 的区别 值得记录的一些工作近况 | Hsu Yeung 的博客 周传雄成都演唱会 | Hsu Yeung 的博客 并行操作导致获取数据库连接超时 | Hsu Yeung 的博客 风信子 | Hsu Yeung 的博客 Linux 安装并配置 Nginx | Hsu Yeung 的博客 使用 logrotate 切割 nginx 日志 郁金香土培结果 | Hsu Yeung 的博客 MySQL LEFT JOIN 右表有多条数据但只取最新的一条 | Hsu Yeung 的博客 获取数据库锁等待超时问题 | Hsu Yeung 的博客 从零开始挑选相机 | Hsu Yeung 的博客 郁金香 | Hsu Yeung 的博客 我的第一束花 | Hsu Yeung 的博客 快乐的一周 | Hsu Yeung 的博客 重庆,来了 | Hsu Yeung 的博客 散步 | Hsu Yeung 的博客 Git 工作区、暂存区、版本库之间的关系 | Hsu Yeung 的博客
MySQL FIND_IN_SET 函数的使用 | Hsu Yeung 的博客
2023-07-27 · via Hsu Yeung 的博客

FIND_IN_SET 函数在 MySQL 官方文档 中的定义如下:


  • FIND_IN_SET(str, strlist)

    Returns a value in the range of 1 to N if the string str is in the string list strlist consisting of N substrings. A string list is a string composed of substrings separated by , characters. If the first argument is a constant string and the second is a column of type SET, the FIND_IN_SET() function is optimized to use bit arithmetic. Returns 0 if str is not in strlist or if strlist is the empty string. Returns NULL if either argument is NULL. This function does not work properly if the first argument contains a comma (,) character.

大概意思就是,FIND_IN_SET(str, strlist) 函数接受两个参数:

第一个参数 str 是要查找的字符串。第二个参数 strlist 是要搜索的用 ,(英文逗号) 分隔的字符串。

  • 如果 strstrlist 为 NULL,则函数返回 NULL
  • 如果 strlist 中不包含 str,则返回 0
  • 如果 strlist 中包含 str,则返回一个在 1-N 之间的正整数(N 为按照逗号分隔的元素数量),这个整数就表示 strstrlist 中的第几个

需要注意的是,如果 str 中包含了 ,,该函数将无法正常工作(实测返回值为 0,表示没找到)。并且,如果 str 是一个常量字符串,而且 strlist 是一个类型为 SET 的列,MySQL 将会使用使用位算术来优化查找性能。

来自官方的例子:

SELECT FIND_IN_SET('b', 'a,b,c,d') AS result;
查询结果
查询结果

使用起来很简单易懂,在实际项目中用到的地方也有不少,比如最近项目中需要查询一个地区其下的所有子地区,每个地区都有一个 parent_code 字段标记自己的直接父级是谁,还有一个 path_code 字段存储了从最顶级到当前地区的层级 code 字符串,用逗号分隔的。由于地区不是只有两级,所以没法简单又直接的根据 parent_code 字段实现这个需求.通过 LIKE path_code 的形式来模糊查询也不准确,比如我关键词为 1,那 11,111 这种格式的数据都会被查到,这个时候就需要用到 FIND_IN_SET 函数了,就是专门处理这种逗号分隔的字符串搜索的,最终的实现 SQL 如下:

SELECT code, name, path_code
FROM sys_region_info
WHERE FIND_IN_SET(#{regionCode}, path_code);

这里以 #{regionCode} = 26(四川省的 code)为例,查询四川省下的所有子地区,查询出来的结果如下:

部分查询结果截图
部分查询结果截图

由于表的设计,查询结果里是包含了 code = 26 自己的,如果不需要查询出自己的话可以再稍微修改一下这条 SQL 即可:

SELECT code, name, path_code
FROM sys_region_info
WHERE FIND_IN_SET(#{regionCode}, path_code)
AND path_code != #{regionCode}