当前位置:网站首页>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 !
边栏推荐
- CUDA exploration
- Matlab tips (27) grey prediction
- With the help of this treasure artifact, I became the whole stack
- Dockermysql modifies the root account password and grants permissions
- 自动更新Selenium驱动chromedriver
- European Bioinformatics Institute 2021 highlights report released: nearly 1million proteins have been predicted by alphafold
- Is "applet container technology" a gimmick or a new outlet?
- js导入excel&导出excel
- Hard core observation 545 50 years ago, Apollo 15 made a feather landing experiment on the moon
- Cloud native (32) | kubernetes introduction to platform storage system
猜你喜欢
mysql连接vscode成功了,但是报这个错
(shuttle) navigation return interception: willpopscope
ACL 2022 | small sample ner of sequence annotation: dual tower Bert model integrating tag semantics
Hard core observation 545 50 years ago, Apollo 15 made a feather landing experiment on the moon
Case recommendation: An Qing works with partners to ensure that the "smart court" is more efficient
European Bioinformatics Institute 2021 highlights report released: nearly 1million proteins have been predicted by alphafold
MySQL authentication bypass vulnerability (cve-2012-2122)
Koa2 addition, deletion, modification and query of JSON array
JS addition, deletion, modification and query of JSON array
Cover fake big empty talk in robot material sorting
随机推荐
DockerMySQL无法被宿主机访问的问题解决
AI表现越差,获得奖金越高?纽约大学博士拿出百万重金,悬赏让大模型表现差劲的任务...
MySQL中正则表达式(REGEXP)使用详解
为了交通安全,可以做些什么?
Some suggestions for foreign lead2022 in the second half of the year
Motion capture for snake motion analysis and snake robot development
koa2对Json数组增删改查
The problem of ASP reading Oracle Database
CRMEB商城系统如何助力营销?
Huawei cloud gaussdb (for redis) unveils issue 21: using Gauss redis to achieve secondary indexing
不要再说微服务可以解决一切问题了
同一个作业有两个source,同一链接不同数据库账号,为何第二个链接查出来的数据库列表是第一个账号的
ACL 2022 | small sample ner of sequence annotation: dual tower Bert model integrating tag semantics
[launched in the whole network] redis series 3: high availability of master-slave architecture
Koa2 addition, deletion, modification and query of JSON array
MySQL authentication bypass vulnerability (cve-2012-2122)
浅谈现在的弊端与未来的发展
View
企業不想換掉用了十年的老系統
B站大佬用我的世界搞出卷積神經網絡,LeCun轉發!爆肝6個月,播放破百萬