当前位置:网站首页>Example code of MySQL split string as query condition
Example code of MySQL split string as query condition
2022-07-06 23:20:00 【1024 questions】
mysql.help_topic
REPLACE
LENGHT
substring_index
analysis
A group friend asked a question
Of this watch ancestors Columns store all ancestor nodes , With ,
Separate
For example, I query dept_id by 103 All ancestor nodes of , Now I have only one dept_id How to find out
Then I went online to find such a magical sql, Changing the name of the watch becomes the following
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
Um. , Yes, the result came out , Then I was curious , What principle? , One by one
mysql.help_topicThis is a mysql The self-contained help explains the annotation table , The query results are as follows
id from 0 Start , My version is the largest id To 584, Different versions should id The maximum value is also different , The function of this watch will be explained later
REPLACEThis function should be known , For replacing characters
LENGHTGets the length of the string
substring_indexQuery string , Three parameters , The string to split , According to the split characters , From the first few
If the last parameter is positive, count from the left , Then get all the characters to the left of the corresponding subscript
If it's negative , Then count from the right , Get all strings to the right of the corresponding subscript , This is not a demonstration
analysisLet's look at the first paragraph
( length( a.ancestors ) - length( REPLACE ( a.ancestors, ',', '' ) ) + 1 )
We assume that the current data ancestors The value is 0,100,101
So the first one length(a.ancestors)
The value is 9 Subtract the following paragraph
length( REPLACE ( a.ancestors, ',', '' ) )
Because we assume that there are two values ,
therefore length by 7 Finally, add 1 Then this value is 3
And the one in front join on The data that can be found by conditions is mysql.help_topic
All in this table id Less than 3 The data of , That is to say id by 0,1,2 Three pieces of data
Now let's take a look at the results of this query
So let's assume that now is the first line ,mysql.help_topic
In the table help_topic_id
by 0
substring_index( substring_index( a.ancestors, ',', b.help_topic_id + 1 ), ',',- 1 )
Innermost substring_index After break up to 0, Because there is nothing that can be split, so the outside substring_index And back again 0
The second line help_topic_id
by 1 The result is 0,100
Then execute the outer substring_index according to ,
Split , The value is -1 So find one on the right , The value obtained is 100
The third line results in 0,100,101
, Outer layer substring_index After execution, the result is 101
.... You can only shout in your heart
Now I know mysql.help_topic
Does this watch work ? It is used to branch the split data , Professionally, it's called The cartesian product
( I really don't understand ..)
This method also has disadvantages : That is, the number of rows cannot be greater than mysql.help_topic
The number of data entries in this table
This is about mysql This is the end of the article on the example code of splitting strings as query conditions , More about mysql Split string query content, please search the previous articles of software development network or continue to browse the relevant articles below. I hope you will support software development network more in the future !
边栏推荐
- Automatically update selenium driver chromedriver
- 专为决策树打造,新加坡国立大学&清华大学联合提出快速安全的联邦学习新系统
- Pytest unit test series [v1.0.0] [pytest execute unittest test case]
- 云原生(三十二) | Kubernetes篇之平台存储系统介绍
- Cloud native (32) | kubernetes introduction to platform storage system
- Docker mysql5.7 how to set case insensitive
- View
- TDengine 社区问题双周精选 | 第二期
- How to choose indoor LED display? These five considerations must be taken into account
- CSDN 上传图片取消自动加水印的方法
猜你喜欢
CUDA exploration
Custom swap function
Modules that can be used by both the electron main process and the rendering process
Cloud native technology container knowledge points
MySQL实现字段分割一行转多行的示例代码
Improving Multimodal Accuracy Through Modality Pre-training and Attention
DR-Net: dual-rotation network with feature map enhancement for medical image segmentation
借助这个宝藏神器,我成为全栈了
ACL 2022 | small sample ner of sequence annotation: dual tower Bert model integrating tag semantics
案例推荐丨安擎携手伙伴,保障“智慧法院”更加高效
随机推荐
石墨文档:4大对策解决企业文件信息安全问题
浅谈网络安全之文件上传
MySQL数据库之JDBC编程
Unified Focal loss: Generalising Dice and cross entropy-based losses to handle class imbalanced medi
CRMEB商城系统如何助力营销?
[launched in the whole network] redis series 3: high availability of master-slave architecture
(DART) usage supplement
Why are some people still poor and living at the bottom of society even though they have been working hard?
Automatically update selenium driver chromedriver
CSDN 上传图片取消自动加水印的方法
PDF批量拆分、合并、书签提取、书签写入小工具
安全保护能力是什么意思?等保不同级别保护能力分别是怎样?
#DAYU200体验官# 首页aito视频&Canvas绘制仪表盘(ets)
允许全表扫描 那个语句好像不生效set odps.sql.allow.fullscan=true;我
ICLR 2022 | pre training language model based on anti self attention mechanism
Let me ask you if there are any documents or cases of flynk SQL generation jobs. I know that flynk cli can create tables and specify items
Unified Focal loss: Generalising Dice and cross entropy-based losses to handle class imbalanced medi
Two week selection of tdengine community issues | phase II
js对JSON数组的增删改查
asp读取oracle数据库问题