mysql Split string as query condition
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
SELECT
substring_index( substring_index( a.ancestors, ',', b.help_topic_id + 1 ), ',',- 1 ) AS shareholder
FROM
sys_dept a
JOIN mysql.help_topic b ON b.help_topic_id < ( length( a.ancestors ) - length( REPLACE ( a.ancestors, ',', '' ) ) + 1 )
WHERE
dept_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



![[yolov3 loss function]](/img/79/87bcc408758403cf3993acc015381a.png)
![[upsampling method opencv interpolation]](/img/6b/5e8f3c2844f0cbbbf03022e0efd5f0.png)




