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. New and old domain name jump Application scena...
Table of contents 1. Project requirements 2. Docu...
It is very convenient to connect to a remote serv...
This article shares the installation and configur...
[LeetCode] 175.Combine Two Tables Table: Person +...
I was in a meeting when a colleague called to rep...
1. Overlay Overview Overlay means covering, as th...
The official source code of monaco-editor-vue is ...
The significance of writing order Reduce browser ...
Today, when I was using Nginx, a 500 error occurr...
Table of contents 1. Static implementation method...
This article describes how to install Apache on a...
When using Docker containers, it is more convenie...
Preface Interceptor In some modern front-end fram...
When doing database statistics, you often need to...