Preface This is a new function I came across recently. My usage scenario in the project is as follows: There is an application type table, which contains parentId field and parentIds field. The former is the parent id, and the latter is the multi-level parent id. Both are varchar type in the database. parentIds is composed of multiple parent ids and separated by "". The team leader told me that I could use this function, so I went to learn about it and use it. grammar
definition
strlist: A string linked by English commas "," for example: "a,b,c,d". The string is similar to the SET type value linked by commas. Example: SELECT FIND_IN_SET('b','a,b,c,d'); //The return value is 2, which is the second value example There is a type field in the article table, which stores the article type, including 1 headline, 2 recommendation, 3 hot spot, 4 picture and text, etc. This is where find_in_set comes in handy. The following is the quoted content: select * from article where FIND_IN_SET('4',type) The difference between find_in_set() and in: Make a test table to illustrate the difference between the two CREATE TABLE `tb_test` ( `id` int(8) NOT NULL auto_increment, `name` varchar(255) NOT NULL, `list` varchar(255) NOT NULL, PRIMARY KEY (`id`) ); INSERT INTO `tb_test` VALUES (1, 'name', 'daodao,xiaohu,xiaoqin'); INSERT INTO `tb_test` VALUES (2, 'name2', 'xiaohu,daodao,xiaoqin'); INSERT INTO `tb_test` VALUES (3, 'name3', 'xiaoqin,daodao,xiaohu'); I originally thought that MySQL could perform such a query: SELECT id,name,list from tb_test WHERE 'daodao' IN(list); -- (I) In fact, this does not work. The query is only valid when the value of the list field is equal to 'daodao' (completely matching the string before IN), otherwise no results will be obtained, even if 'daodao' is really in the list. Let’s look at this: SELECT id,name,list from tb_test WHERE 'daodao' IN ('libk', 'zyfon', 'daodao'); -- (II) That's fine. What is the difference between these two? Why the first one cannot get the correct result, but the second one can. The reason is that (list) list in (1) is a variable, while ('libk', 'zyfon', 'daodao') in (2) are constants. So if you want (1) to work correctly, you need to use find_in_set(): SELECT id,name,list from tb_test WHERE FIND_IN_SET('daodao',list); -- Improved version of (I) Summarize This is the end of this article about the basic usage of the find_in_set function in MySQL. For more information about the usage of the MySQL find_in_set function, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: How to install and configure the supervisor daemon under centos7
>>: Vue.js uses Element-ui to implement the navigation menu
1. Introduction Some time ago, there were a serie...
Table of contents Preface The role of deconstruct...
There was a shaking barrage on TikTok a while ago...
Table of contents 1. Keywords 2. Deconstruction 3...
At this time, you can use overflow:auto; (when the...
Union is a union operation on the data, excluding...
Database SQL optimization is a common problem. Wh...
When I turned on my MAC at night, I found that th...
Table of contents redo log Why do we need to upda...
This article example shares the specific code of ...
Table of contents In vue2 In vue3 Notes on setup ...
The final solution is in the last picture If you ...
Preface Dockerfile is a script interpreted by the...
1. Install cmake 1. Unzip the cmake compressed pa...
Table of contents background Problem Description ...