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
1 | name | daodao, xiaohu, xiaoqin | |||
2 | name2 | xiaohu, daodao, xiaoqin | |||
3 | name3 | xiaoqin, daodao, xiaohu |
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.
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';
|
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
1 | name | daodao, xiaohu, xiaoqin | |||
2 | name2 | xiaohu, daodao, xiaoqin | |||
3 | name3 | xiaoqin, daodao, xiaohu |
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.
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';
|