当前位置:网站首页> mysql拆分字符串作为查询条件的示例代码
mysql拆分字符串作为查询条件的示例代码
2022-07-06 15:36:00 【1024问】
mysql.help_topic
REPLACE
LENGHT
substring_index
分析
有个群友问一个问题

这表的ancestors列存放的是所有的祖先节点,以,分隔
例如我查询dept_id为103的所有祖先节点,现在我只有一个dept_id该怎么查
然后我去网上找到这样一个神奇的sql,改改表名就成了下面的这样
SELECTsubstring_index( substring_index( a.ancestors, ',', b.help_topic_id + 1 ), ',',- 1 ) AS shareholder FROMsys_dept aJOIN mysql.help_topic b ON b.help_topic_id < ( length( a.ancestors ) - length( REPLACE ( a.ancestors, ',', '' ) ) + 1 ) WHEREdept_id = 103 
嗯,没错结果出来了,然后我就很好奇,什么原理,一个个来看
mysql.help_topic这个是一个mysql自带的帮助解释注释表,查询结果如下

id从0开始,我这个版本最大id到584,版本不同应该id最大值也不一样,这个表的作用一会说
REPLACE这个函数应该都知道吧,替换字符用的

获取字符串的长度

查分字符串,三个参数,要拆分的字符串,根据拆分的字符,从第几个开始
如果最后的那个参数为正数则从左开始数,然后获取对应下标左边的所有字符
如果为负数,则从右边开始数,获取对应下标右边的所有字符串,这个就不演示了


先来看第一段
( length( a.ancestors ) - length( REPLACE ( a.ancestors, ',', '' ) ) + 1 )我们假设当前数据的ancestors值为 0,100,101 那么第一个length(a.ancestors)的值就是9 减去后面的一段
length( REPLACE ( a.ancestors, ',', '' ) ) 因为我们假设的值里面有两个 , 所以length为7 最后在加1 那么这段值为 3
和前面的 join on条件能查出的数据也就是mysql.help_topic这个表中所有id小于3的数据,也就是id为0,1,2的三条数据
那么现在先来看看这样查询的结果是啥

那么我们假设现在是第一行,mysql.help_topic表中的help_topic_id为0
substring_index( substring_index( a.ancestors, ',', b.help_topic_id + 1 ), ',',- 1 )最里面的substring_index 拆分后为0,因为没有能在拆分的了所以外面的substring_index返回的也是0
第二行help_topic_id为1的时候获取结果为0,100
然后执行外层的substring_index 根据,拆分,值为-1 所以从右边找一位,获取的值就是100
第三行结果为0,100,101,外层substring_index 执行后结果为101
....只能心中大喊牛逼
那么现在知道mysql.help_topic这个表的作用了吗?就是用来对拆分出的数据分行,专业点叫笛卡尔积 (真的不懂..)
这种方法也有缺点:就是拆出的行数不能大于mysql.help_topic这个表的数据条数
到此这篇关于mysql拆分字符串作为查询条件的示例代码的文章就介绍到这了,更多相关mysql拆分字符串查询内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!
边栏推荐
- Improving Multimodal Accuracy Through Modality Pre-training and Attention
- [unity] upgraded version · Excel data analysis, automatically create corresponding C classes, automatically create scriptableobject generation classes, and automatically serialize asset files
- 2014 Alibaba web pre intern project analysis (1)
- 2014阿里巴巴web前实习生项目分析(1)
- Sword finger offer question brushing record 1
- POJ 1258 Agri-Net
- How big is the empty structure?
- 浅谈网络安全之文件上传
- Balanced Multimodal Learning via On-the-fly Gradient Modulation(CVPR2022 oral)
- Introduction to network basics
猜你喜欢

Rust knowledge mind map XMIND

Leetcode exercise - Sword finger offer 26 Substructure of tree

Aardio - does not declare the method of directly passing float values

Leetcode: interview question 17.24 Maximum cumulative sum of submatrix (to be studied)

MATLAB小技巧(27)灰色预测

ICLR 2022 | pre training language model based on anti self attention mechanism

ACL 2022 | 序列标注的小样本NER:融合标签语义的双塔BERT模型

Sword finger offer question brushing record 1

动作捕捉用于蛇运动分析及蛇形机器人开发

Improving Multimodal Accuracy Through Modality Pre-training and Attention
随机推荐
关于声子和热输运计算中BORN电荷和non-analytic修正的问题
TypeScript获取函数参数类型
视图(view)
Custom swap function
Thinkphp5 multi table associative query method join queries two database tables, and the query results are spliced and returned
On file uploading of network security
MySQL教程的天花板,收藏好,慢慢看
儿童睡衣(澳大利亚)AS/NZS 1249:2014办理流程
dockermysql修改root账号密码并赋予权限
[compilation principle] LR (0) analyzer half done
专为决策树打造,新加坡国立大学&清华大学联合提出快速安全的联邦学习新系统
Improving Multimodal Accuracy Through Modality Pre-training and Attention
DR-Net: dual-rotation network with feature map enhancement for medical image segmentation
Use ECs to set up an agent
Word2vec (skip gram and cbow) - pytorch
Const keyword
C three ways to realize socket data reception
NPM cannot install sharp
树的先序中序后序遍历
Cloud native technology container knowledge points