当前位置:网站首页>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

Catalog

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_topic

This 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

REPLACE

This function should be known , For replacing characters

LENGHT

Gets the length of the string

substring_index

Query 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

analysis

Let'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 !


原网站

版权声明
本文为[1024 questions]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207061505471153.html