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
mysql More related articles about splitting strings for conditional queries
- mysql Additions and deletions —— Conditions of the query + Fuzzy query
Condition query is generally = be equal to > Greater than < Less than >= Greater than or equal to <= Less than or equal to <> Section between and Section or also and perhaps in contain like Fuzzy query example , ...
- mysql How to count the number of records after querying according to groups and conditions
1. Subquery , The queried data can be aliased randomly , Then the data queried according to the grouping and conditions , As a table with one column , Then the external query queries the total number of this column of the data table , that will do . SELECT COUNT( * ) FROM ( ...
- use Set Do conditional query for elements in
A teacher teaches many students , A student is taught by many teachers , A student has many books , The same kind of book is owned by many students . Inquiry teaching has books "a" Teachers of students : class teacher{ String id; ...
- mysql where Statement multi condition query is and and or A combination of bug
There is a segment function in the company project, which is the joint query of the global search box and the drop-down list , In the development of joint commissioning, it is found that it is correct to use drop-down query alone , It is also correct to use the global search box alone , The test found that sql There's something wrong with the grammar . Screenshot of the problem : The core of the problem is that the search box is used for multiple ...
- mysql String splicing in , Inquire about sql Sentence summary
DELIMITER $$ USE `ld_wpfmgl_sys`$$ DROP PROCEDURE IF EXISTS `code_query`$$ CREATE DEFINER=`root`@`%` ...
- laravel Read memcached Cache and make conditional queries
public function onlineplayersource() { $res = $_POST['aoData']; $sEcho = 0; $iDisplayStart = 0; // rise ...
- mysql Single table condition query of
create table staff_info( id int primary key auto_increment, name varchar(32) not null, age int(3) un ...
- linq String search criteria , Sorting conditions -linq Dynamic query statements Dynamic LINQ
When it comes to searching and sorting , It's usually the string from the front desk that makes the condition , The number of parameters is uncertain , This requires spelling sql Spell it like a sentence linq sentence . and linq Statements are strongly typed , You can't spell it with strings . Ok now , There's an open source linq Extension method ...
- MySQL Break down altogether 9: Sorting of queries 、 Paging related
summary The data in the database is presented directly, which is generally not what we want , So the last two sections showed how to filter data . In addition to filtering the data , We may also need to sort the data , For example, if you want to know the most expensive items from the list , You may need to write the amount ...
- Mysql Master Series - The first 7 piece : Get along well with select Conditions of the query , Avoid trampling
This is a Mysql The first series 7 piece . Environmental Science :mysql5.7.25,cmd Demo in command . In e-commerce : We want to see all the orders of a certain user , Or want to view all orders of a user in a certain period of time , At this time, we need to filter the order table data , Press ...
Random recommendation
- MSSQL Additional database 5120 error ( Access denied ) processing method
One . Right click the database file you want to attach , The properties dialog box pops up , Select the security tab . find Authenticated Users user name . If not found , Conduct Authenticated Users Add user name . Two . add to Au ...
- lua The stack
lua The stack is from the bottom to the top : lua_pushstring(L, "test1");lua_pushstring(L, "test2");lua_pushstri ...
- android menu The problem of
1. Easy to use @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.me ...
- c Function and pointer learning 7
1. Storage allocation of structure 1 2 printf("%d \n",sizeof(char)); printf("%d \n",sizeof(int)); int The type is ...
- About haproxy hdr_reg(host) Some explanations for
I've recently taken over an environment using HAProxy, and I'm attempting to learn the config and wh ...
- Java Summary of multithreading programming ( The essence of )
Java Summary of multithreading programming 2007-05-17 11:21:59 label : Multithreading java Original works , Allowed to reprint , When reprinting, please make sure to indicate the article in the form of hyperlink Original source . Author information and this statement . Otherwise, the legal liability will be investigated .http ...
- Notes on Qingfeng -Swift Programming language
Preface Apple A brand new Swift Programming language , To develop iOS and OS X Platform applications . Its purpose is self-evident : Just for the old Objective-C A suitable successor ! therefore , Not hard to foresee , future ...
- java Socket Realize simple online chat ( Two )
Pick up <java Socket Realize simple online chat ( One )>, On the basis of single client connection , The second step here is to realize the connection of multiple clients , You need to use threads . Whenever a new client connects , The server needs to start a new thread to enter ...
- bhttpd
In the past, the product application used serial port as console , Wrote a console with simple command history and command completion function Shell, Used for program debugging , Including viewing system status, debugging, modifying settings, etc . It's really easy to use , It has played a good role in the rapid positioning of many simple on-site problems . system ...
- Python Learn Chapter 16 —— exception handling
In practice, , Many times , We cannot guarantee that the program we write is perfect . For example, the original intention of our program is : The user enters a number in the input box , And carry out subsequent mathematical operations , Even if we remind users that they need to enter numbers instead of text , But sometimes unintentionally or maliciously enter characters ...