MySQL-FIND_IN_SET () function of the FIND_IN_SET MySQL () function

MySQL's FIND_IN_SET () function

Today in doing the project, I saw a MySQL function --FIND_IN_SET never seen (), suddenly had a strong interest in, and then search the search, rolled.

Syntax: FIND_IN_SET (str, strlist)

Definitions :

1. If the string str N sub-chains by a string list strlist, the value returned in the range between 1 to N.

2 is a list of strings is a string consisting of a number ',' symbol separate from the chain.

3. If the first parameter is a constant string, and the second is typeSET column, the FIND_IN_SET () function is optimized, the use of bits is calculated.

4. If not strlist str strlist or empty string, the return value is 0.

5. any parameter is NULL, return value is NULL. This will not work when the first argument function contains a comma ( ',').

strlist : a comma a "," link string, for example: "a, b, c, d", similar to the string form of the value of SET type is linked to a comma.

Example: the SELECT the FIND_IN_SET ( 'B', 'A, B, C, D');  // return value 2, i.e. the second value

 

A more detailed article: http://blog.sina.com.cn/s/blog_5b5460eb0100e5r9.html

 

mysql the IN and FIND_IN_SET of inquiries

 

Mysql originally thought such a query can
select id, list, name from table where 'daodao' IN (list); ( a)
Note:. 1 table comprising three fields id: int, list: varchar ( 255), name: varchar (255)

in fact this is not acceptable, so only when the 'daodao' is the first element in the list (I was the first test of seemingly is not enough, is only when the value list field is equal daodao right), the query is valid, otherwise the results are less than even 'daodao' really in the list

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`);
The results obtained null value.
test2:sql = select * from `test` where FIND_IN_SET('daodao',`list`);
Get three 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';
Then execute sql test1, you can return a result.

Let's look at this:
SELECT ID, List, from Table name WHERE 'daodao' the IN ( 'libk', 'zyfon', 'daodao'); (ii)
this is possible
----------- ----------------------------------------------

these two in the end what differences are there? Why not first get the right results, while the second was able to get results.

In fact, the reason is (a) in the (list) list is variable, and (II) ( 'libk', 'zyfon' , 'daodao') is constant

so if you want (a) to work correctly, you need to use find_in_set () :
SELECT ID, List, Table WHERE name from the FIND_IN_SET ( 'daodao', List); (a) improved version.
 
Summary: So if the list is a constant, it can be directly used IN, otherwise use FIND_IN_SET () function
           
         
  • FIND_IN_SET(str,strlist)
If the string str strlist a string list consisting of N sub-chains, the return value in the range between 1 to N. A string is a list of some of the string ',' symbol separate from the chain. If the first parameter is a constant string, and the second is a column type SET, then the FIND_IN_SET () function is optimized using bit calculation. If not strlist str strlist or empty string, return value is zero. As any parameter is NULL, return value is NULL. This will not work when the first argument function contains a comma ( ','). 

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

        -> 2

Extended usage by ordering FIND_IN_set

eg:

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

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

 

Today in doing the project, I saw a MySQL function --FIND_IN_SET never seen (), suddenly had a strong interest in, and then search the search, rolled.

Syntax: FIND_IN_SET (str, strlist)

Definitions :

1. If the string str N sub-chains by a string list strlist, the value returned in the range between 1 to N.

2 is a list of strings is a string consisting of a number ',' symbol separate from the chain.

3. If the first parameter is a constant string, and the second is typeSET column, the FIND_IN_SET () function is optimized, the use of bits is calculated.

4. If not strlist str strlist or empty string, the return value is 0.

5. any parameter is NULL, return value is NULL. This will not work when the first argument function contains a comma ( ',').

strlist : a comma a "," link string, for example: "a, b, c, d", similar to the string form of the value of SET type is linked to a comma.

Example: the SELECT the FIND_IN_SET ( 'B', 'A, B, C, D');  // return value 2, i.e. the second value

 

A more detailed article: http://blog.sina.com.cn/s/blog_5b5460eb0100e5r9.html

 

mysql the IN and FIND_IN_SET of inquiries

 

Mysql originally thought such a query can
select id, list, name from table where 'daodao' IN (list); ( a)
Note:. 1 table comprising three fields id: int, list: varchar ( 255), name: varchar (255)

in fact this is not acceptable, so only when the 'daodao' is the first element in the list (I was the first test of seemingly is not enough, is only when the value list field is equal daodao right), the query is valid, otherwise the results are less than even 'daodao' really in the list

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`);
The results obtained null value.
test2:sql = select * from `test` where FIND_IN_SET('daodao',`list`);
Get three 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';
Then execute sql test1, you can return a result.

Let's look at this:
SELECT ID, List, from Table name WHERE 'daodao' the IN ( 'libk', 'zyfon', 'daodao'); (ii)
this is possible
----------- ----------------------------------------------

these two in the end what differences are there? Why not first get the right results, while the second was able to get results.

In fact, the reason is (a) in the (list) list is variable, and (II) ( 'libk', 'zyfon' , 'daodao') is constant

so if you want (a) to work correctly, you need to use find_in_set () :
SELECT ID, List, Table WHERE name from the FIND_IN_SET ( 'daodao', List); (a) improved version.
 
Summary: So if the list is a constant, it can be directly used IN, otherwise use FIND_IN_SET () function
           
         
  • FIND_IN_SET(str,strlist)
If the string str strlist a string list consisting of N sub-chains, the return value in the range between 1 to N. A string is a list of some of the string ',' symbol separate from the chain. If the first parameter is a constant string, and the second is a column type SET, then the FIND_IN_SET () function is optimized using bit calculation. If not strlist str strlist or empty string, return value is zero. As any parameter is NULL, return value is NULL. This will not work when the first argument function contains a comma ( ','). 

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

        -> 2

Extended usage by ordering FIND_IN_set

eg:

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

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

 

Guess you like

Origin www.cnblogs.com/shishibuwan/p/12027856.html