当前位置:网站首页>Mysql Find_ IN_ Set function

Mysql Find_ IN_ Set function

2022-06-24 02:18:00 Dong Dong Dong Dong Dong Dong Dong


While working on the project today , I saw one I've never seen before MySQL function ——FIND_IN_SET(), There was a strong interest immediately , And then I searched , Turn over .

grammar :​FIND_IN_SET(str,strlist)

Definition ​:

1. If ​ character string str​ In by ​N​ Composed of sub chains ​ String list strlist​ in , Then the return value range is 1 To ​N​ Between .

2. A list of strings is a list of characters that are ‘,’ A string of symbols separated from a chain .

3. If the first parameter is a constant string , And the second is typeSET Column , be FIND_IN_SET() Functions are optimized , Use ​ The bit ​ Calculation .

4. If ​str​ be not in ​strlist​ or ​strlist​ Is an empty string , The return value is 0.

5. If any parameter is NULL, The return value is NULL. This function contains a comma in the first argument (‘,’) Will not work properly .

strlist​: An English comma “,” Linked string , for example :"a,b,c,d", The string is similar in form to SET The values of types are linked by commas .

notes : The fields must be separated by commas , Tested by bloggers in person , Fields separated by Chinese commas will not be searched , Even if the field contains the string to search

Example :​SELECT FIND_IN_SET('b','a,b,c,d'); ​// The return value is 2, That is to say 2 It's worth

mysql Medium  IN and FIND_IN_SET Query questions for

I thought mysql You can do this query

select id, list, name from table where 'daodao' IN (list);      ( One )

notes :1. table There are three fields id:int,  list:varchar(255),  name:varchar(255)


In fact, this is not the case , This is only when 'daodao' yes list The first element in ( When I tested it, it seemed that I was the first one, but I couldn't either , Only when list The value of the field is equal to daodao That's right ) when , The query is valid , Otherwise, none of them will come to an end , Even if 'daodao' It's really list in


Test code :

CREATE TABLE `test` (

  `id` int(8) NOT NULL auto_increment,

  `name` varchar(255) NOT NULL,

  `list` varchar(255) NOT NULL,

  PRIMARY KEY  (`id`)

)


INSERT INTO `test` VALUES (1, 'name', 'daodao,xiaohu,xiaoqin');

INSERT INTO `test` VALUES (2, 'name2', 'xiaohu,daodao,xiaoqin');

INSERT INTO `test` VALUES (3, 'name3', 'xiaoqin,daodao,xiaohu');


test1:sql = select * from `test` where 'daodao' IN (`list`);

Get result null .

test2:sql = select * from `test` where FIND_IN_SET('daodao',`list`);

Get three pieces of data .





1

name

daodao,xiaohu,xiaoqin




2

name2

xiaohu,daodao,xiaoqin




3

name3

xiaoqin,daodao,xiaohu

Modify table data

update `test` set `list`='daodao' where `id`='1';

And then execute test1 Of sql, You can return a result .

Let's take a look at this :

select id, list, name from table where 'daodao' IN ('libk', 'zyfon', 'daodao');    ( Two )

This is OK

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


What's the difference between these two ? Why can't Article 1 get the right result , And the second one can get results .


The reason is ( One ) in (list)    list It's a variable. , and ( Two ) in   ('libk', 'zyfon', 'daodao') Is a constant


So if we want to let ( One ) Can work correctly , Need to use find_in_set():

select id, list, name from table where FIND_IN_SET( 'daodao' , list);  ( One ) Improved version .


summary : So if list Is a constant , You can use IN, Otherwise, use FIND_IN_SET() function














  • FIND_IN_SET(​str​,​strlist​)

If the string ​str​  In by ​N​  List of strings made up of sub chains ​strlist​  in , Then the return value range is  1  To  ​N​  Between . A list of strings is a list of characters that are ‘,’ A string of symbols separated from a chain . If the first parameter is a constant string , And the second is type SET Column , be    FIND_IN_SET()  Functions are optimized , Use bit calculation . If ​str​ be not in ​strlist​  or ​strlist​  Is an empty string , The return value is  0 . If any parameter is NULL, The return value is NULL. This function contains a comma in the first argument (‘,’) Will not work properly . 


mysql> ​SELECT FIND_IN_SET('b','a,b,c,d');

        -> 2

Extend usage , utilize FIND_IN_set Sort

eg:

$ids = '9,3,45,1,8,2,6';

$sql = "... WHERE goods_id IN('{$ids}') ORDER BY FIND_IN_SET(goods_id, '{$ids}')";





原网站

版权声明
本文为[Dong Dong Dong Dong Dong Dong Dong]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211642590134.html

随机推荐