当前位置:网站首页>Find in MySQL_ in_ Detailed explanation of set() function usage
Find in MySQL_ in_ Detailed explanation of set() function usage
2022-07-05 18:33:00 【fastjson_】
1、 Official meaning (MySQL Grammar instructions in the manual )
FIND_IN_SET(str,strlist), This function is used to query fields (strlist) Include in (str) Result , The return result is null Or record .
str String to query
strlist Fields to query , Parameter with ”,” Separate , In the form of (1,2,6,8,10,22)
If the string str In by N A list of strings composed 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 consisting of sub chains separated by symbols .
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 .
Example :
SELECT FIND_IN_SET('b', 'a,b,c,d');
// result :2
// because b stay strlist Collection 2 The location of , a Is the position 1
select FIND_IN_SET('1', '1');
// result :1
// By this time strlist The set is a little special , There is only one string
select FIND_IN_SET('2', '1,2');
// result :2
select FIND_IN_SET('6', '1');
// result :0 strlist Does not exist in the str, So back 0.Sum up : FIND_IN_SET Function , If the previous string is contained in the next string set , Return is greater than the 0 Number of numbers , This number is the position of the previous string in the next string .
2、find_in_set() and in The difference between
New test table , Add several test data .
CREATE TABLE `test` (
`ID` int(11) NOT NULL,
`LIST` varchar(255) DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of test
-- ----------------------------
INSERT INTO `test` VALUES ('1', 'AA,BB,CC');
INSERT INTO `test` VALUES ('2', 'AA,BB');
INSERT INTO `test` VALUES ('3', 'AA');find_in_set() and in Query comparison of
-- IN Query field conditions
SELECT id,list,' Field conditions ' AS 'IN Field ' from TEST WHERE list in ('AA');
-- IN Query constant conditions - Condition is true
SELECT id,list,' Condition is true ' AS 'IN Constant condition is true ' from TEST WHERE 'AA' in ('AA','BB');
-- IN Query constant conditions - The condition is false
SELECT id,list,' The condition is false ' AS 'IN Constant condition is false ' from TEST WHERE 'AA' in ('BB','CC');
-- FIND_IN_SET Field conditions
SELECT id,list,' Field conditions ' AS 'FIND_IN_SET Field ' from TEST WHERE FIND_IN_SET('AA', list);
-- FIND_IN_SET Constant condition is true
SELECT id,list,' Condition is true ' AS 'FIND_IN_SET Constant condition is true ' from TEST WHERE FIND_IN_SET('AA', 'AA,BB,CC');
-- FIND_IN_SET Constant condition is false
SELECT id,list,' The condition is false ' AS 'FIND_IN_SET Constant condition is false ' from TEST WHERE FIND_IN_SET('AA', 'BB,CC');
difference :
1、in It can only be followed by a constant , find_in_set() Functions can use constants or fields .
2、in It's a perfect match ,find_in_set() Functions are exact matches , Field values are in English ”,” Separate .
another :like Is a wide range of fuzzy matching , There is no delimiter in the string ,Find_IN_SET It's an exact match , Field values are in English ”,” Separate ,Find_IN_SET The result of the query should be less than like Result of query .
3、 Application scenarios
1、 Article table type A field
There is a type Field , It stores article types , Yes 1 headlines 、2 recommend 、3 hotspot 、4 Graphics and so on . Now there's an article where he's the headline , It's hot again , Or graphics ,type China and Israel 1,3,4 Format store . So how do we use sql Find all type There is 4 What about the graphic type of articles ?
select * from article where FIND_IN_SET('4',type)2、 Department tree query , Match the current node and all child nodes
Data table field description

Matching Department id Or the father id by 100 The data of
SELECT dept_id FROM sys_dept WHERE dept_id = 100 or FIND_IN_SET( 100 , ancestors ) 
边栏推荐
- Use JMeter to record scripts and debug
- node_ Exporter memory usage is not displayed
- U-Net: Convolutional Networks for Biomedical Images Segmentation
- sample_rate(采樣率),sample(采樣),duration(時長)是什麼關系
- Electron安装问题
- Vulnhub's darkhole_ two
- Take a look at semaphore, the current limiting tool provided by JUC
- Tupu software digital twin | visual management system based on BIM Technology
- How to obtain the coordinates of the aircraft passing through both ends of the radar
- Introduction to the development function of Hanlin Youshang system of Hansheng Youpin app
猜你喜欢

About Estimation with Cross-Validation

vulnhub之darkhole_2

Find the first k small element select_ k

Memory management chapter of Kobayashi coding

Use QT to traverse JSON documents and search sub objects

U-Net: Convolutional Networks for Biomedical Images Segmentation

buuctf-pwn write-ups (9)

瞅一瞅JUC提供的限流工具Semaphore

ConvMAE(2022-05)

node_exporter内存使用率不显示
随机推荐
如何获取飞机穿过雷达两端的坐标
U-Net: Convolutional Networks for Biomedical Images Segmentation
Sibling components carry out value transfer (there is a sequence displayed)
图像分类,看我就够啦!
sample_rate(采样率),sample(采样),duration(时长)是什么关系
How to solve the error "press any to exit" when deploying multiple easycvr on one server?
sample_rate(采樣率),sample(采樣),duration(時長)是什麼關系
Use of websocket tool
瞅一瞅JUC提供的限流工具Semaphore
开户注册股票炒股安全吗?有没有风险的?靠谱吗?
Find the first k small element select_ k
Xiaobai getting started with NAS - quick building private cloud tutorial series (I) [easy to understand]
All you want to know about clothing ERP is here
Introduction to Resampling
Image classification, just look at me!
Use of print function in MATLAB
Generate XML schema from class
The 11th China cloud computing standards and Applications Conference | China cloud data has become the deputy leader unit of the cloud migration special group of the cloud computing standards working
Problems encountered in the project u-parse component rendering problems
Is it safe to open an account, register and dig money? Is there any risk? Is it reliable?