Mysql database find_in_set function bitsCN.com
Mysql database find_in_set function
For example:
There is a type field in the document table, which stores the article type, including 1 headlines, 2 recommendations, 3 hotspots, and 4 texts..., 12, and 13.
There is an article that is both Toutiao, hotspot, and graphic,
Type is stored in the format of 1, 3, and 4.
How can we use SQL to search for articles with four text standards in all types ??
This is the time for our find_in_set to crash.
Reference content is as follows:
select * from article where FIND_IN_SET('4',type)
----------------------------------------------------------
Syntax of the find_in_set function in the mysql manual:
FIND_IN_SET (str, strlist)
If the str string is in the strlist consisting of N substrings, the return value ranges from 1 to N.
A string list is a string consisting of substrings separated by commas. If the first parameter is a constant string and the second parameter is the type SET column, the FIND_IN_SET () function is optimized and computed in bits.
If str is not in strlist or strlist is a null string, the return value is 0. If any parameter is NULL, the return value is NULL. This function cannot run normally when the first parameter contains a comma.
mysql> SELECT FIND_IN_SET('b','a,b,c,d');
-> 2 because B's position in the strlist set starts from 1.
--------------------------------------------------------
Select FIND_IN_SET ('1', '1 '); returns 1. in this case, the strlist set is a bit special. only one string requires that the previous string must be in the next string collection to return a number greater than 0.
Select FIND_IN_SET ('2', '1, 2'); returns 2 select FIND_IN_SET ('6', '1'); returns 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.
Id is the field of a table, and each record has an id equal to 1, 2, 3, 4, and 5.
A bit similar to in (set)
select * from treenodes where id in (1,2,3,4,5);
BitsCN.com