Use of find_in_set() function in mysql

First, let's take an example: 
there is a type field in an article table, which stores the article type, including 1 headline, 2 recommendation, 3 hotspot, 4 graphic and so on.
Now there is an article that is both a headline, a hot spot, and a graphic. The type is stored in the format of 1, 3, and 4. So how do we use sql to find all the articles with 4 types of graphic and text types?
It's time for our find_in_set to kick in. The following are quoted:

select * from article where FIND_IN_SET('4',type)

-------------------------------------------------- -------- 
The syntax of the find_in_set function in the MySQL manual:
FIND_IN_SET(str, strlist)

str String to be queried
strlist Field name parameters are separated by "," such as (1, 2, 6, 8) The
query field (strlist) contains the result of (str), the returned result is null or record

If the string str is in the string list strlist consisting of N subchains, the return value is in the range 1 to N. A string list is a string consisting of subchains separated by ',' symbols. If the first argument is a constant string and the second is a type SET column, the FIND_IN_SET() function is optimized to use bit calculations. The return value is 0 if str is not in strlist or if strlist is an empty string. If any parameter is NULL, the return value is NULL. This function will not function properly when the first argument contains a comma (',').

--------------------------------------------------------

 Example:
mysql> SELECT FIND_IN_SET('b', 'a,b,c,d'); 
-> 2 because b is placed at position 2 in the strlist set starting at 1

select FIND_IN_SET('1', '1'); The return is 1. At this time, the strlist set is a bit special. There is only one string. In fact, it is required that the previous string must be in the latter string set to return a number greater than 0. 
select FIND_IN_SET ('2', '1, 2'); return 2 
select FIND_IN_SET('6', '1'); return 0

--------------------------------------------------------

Note: 
select * from treenodes where FIND_IN_SET(id, '1,2,3,4,5'); 
Use the find_in_set function to return multiple records at a time. The 
id is a field of a table, and then each record is id equal to 1, 2 , 3, 4, 5 are 
a bit similar to in (collection) 
select * from treenodes where id in (1,2,3,4,5);

--------------------------------------------------------

The difference between find_in_set() and in:

Get a test sheet to illustrate the difference between the two

copy code
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 ' );
copy code

Originally thought that mysql can make such a query:

SELECT id,name,list from tb_test WHERE 'daodao' IN(list); -- (一) 

In fact, this is not possible, so only when the value of the list field is equal to 'daodao' (which exactly matches the string in front of IN), the query is valid , otherwise no results will be obtained, even if 'daodao' is really in the list .

Take a look at this again:

SELECT id,name,list from tb_test WHERE 'daodao' IN ('libk', 'zyfon', 'daodao'); -- (二)

This is possible.

What is the difference between these two? Why can't the first one get the correct result, but the second one does. The reason is actually that (list) list is a variable in (1), and ('libk', 'zyfon', 'daodao') is a constant in (2).
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 (1)

Summary:
So if the list is a constant, you can use IN directly, otherwise use the find_in_set() function.

--------------------------------------------------------

The difference between find_in_set() and like:

In mysql, sometimes when we query the database, we need to get a record containing a certain value in a certain field, but it cannot be solved by like. Using like may find records we do not want, which is more accurate than like , this time mysql's FIND_IN_SET function comes in handy, let's see an example.

Create table and insert statement:

copy code
CREATE TABLE users(
    id int(6) NOT NULL AUTO_INCREMENT,
    name VARCHAR(20) NOT NULL,
    limits VARCHAR ( 50 ) NOT  NULL , --privilege PRIMARY 
    KEY  ( id)
);

INSERT INTO users(name, limits) VALUES('小张','1,2,12'); 
INSERT INTO users(name, limits) VALUES('小王','11,22,32');
copy code

 Where limits represents the permissions that the user has (separated by commas). Now I want to query the user with permission number 2. If the like keyword is used, the query results are as follows:

SELECT * FROM users WHERE limits LIKE '%2%';

In this way, users who do not have permission '2' for the second piece of data are also found, which does not meet expectations. The following uses the mysql function find_in_set() to solve.

SELECT * FROM users WHERE FIND_IN_SET(2,limits);

This will achieve the effect we expect, and the problem will be solved!

Note : The mysql string function find_in_set(str1, str2) returns the position index of str1 in str2, and str2 must be separated by ",".

Summary: like is a broad fuzzy match, there is no delimiter in the string, Find_IN_SET is an exact match, the field value is separated by English ",", the result of the Find_IN_SET query is smaller than the result of the like query.

--------------------------------------------------------

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325498417&siteId=291194637