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

复制代码
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');
复制代码

原来以为mysql可以进行这样的查询:

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

实际上这样是不行的,这样只有当list字段的值等于'daodao'时(和IN前面的字符串完全匹配),查询才有效,否则都得不到结果,即使'daodao'真的在list中。

再来看看这个:

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

这样是可以的。

这两条到底有什么区别呢?为什么第一条不能取得正确的结果,而第二条却能取得结果。原因其实是(一)中 (list) list是变量, 而(二)中 ('libk', 'zyfon', 'daodao')是常量。
所以如果要让(一)能正确工作,需要用find_in_set():

SELECT id,name,list from tb_test WHERE FIND_IN_SET('daodao',list); -- (一)的改进版

总结:
所以如果list是常量,则可以直接用IN, 否则要用find_in_set()函数。

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

find_in_set()和like的区别:

在mysql中,有时我们在做数据库查询时,需要得到某字段中包含某个值的记录,但是它也不是用like能解决的,使用like可能查到我们不想要的记录,它比like更精准,这时候mysql的FIND_IN_SET函数就派上用场了,下面来看一个例子。

创建表并插入语句:

复制代码
CREATE TABLE users(
    id int(6) NOT NULL AUTO_INCREMENT,
    name VARCHAR(20) NOT NULL,
    limits VARCHAR(50) NOT NULL, -- 权限
    PRIMARY KEY (id)
);

INSERT INTO users(name, limits) VALUES('小张','1,2,12'); 
INSERT INTO users(name, limits) VALUES('小王','11,22,32');
复制代码

 其中limits表示用户所拥有的权限(以逗号分隔),现在想查询拥有权限编号为2的用户,如果用like关键字的话,则查询结果如下:

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

这样第二条数据不具有权限'2'的用户也查出来了,不符合预期。下面利用mysql 函数find_in_set()来解决。

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

这样就能达到我们预期的效果,问题就解决了!

注意:mysql字符串函数 find_in_set(str1,str2)函数是返回str2中str1所在的位置索引,str2必须以","分割开。

总结:like是广泛的模糊匹配,字符串中没有分隔符,Find_IN_SET 是精确匹配,字段值以英文”,”分隔,Find_IN_SET查询的结果要小于like查询的结果。

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

Guess you like

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