当前位置:网站首页>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 )
边栏推荐
- How to solve the error "press any to exit" when deploying multiple easycvr on one server?
- 【PaddleClas】常用命令
- Can communication of nano
- SAP feature description
- 爬虫01-爬虫基本原理讲解
- buuctf-pwn write-ups (9)
- node_exporter内存使用率不显示
- 写作写作写作写作
- New words new words new words new words [2]
- [QNX hypervisor 2.2 user manual]6.3.2 configuring VM
猜你喜欢
【HCIA-cloud】【1】云计算的定义、什么是云计算、云计算的架构与技术说明、华为云计算产品、华为内存DDR配置工具说明
第十一届中国云计算标准和应用大会 | 云计算国家标准及白皮书系列发布 华云数据全面参与编制
爬虫01-爬虫基本原理讲解
buuctf-pwn write-ups (9)
About statistical power
node_exporter内存使用率不显示
FCN: Fully Convolutional Networks for Semantic Segmentation
The 2022 China Xinchuang Ecological Market Research and model selection evaluation report released that Huayun data was selected as the mainstream manufacturer of Xinchuang IT infrastructure!
图像分类,看我就够啦!
Share: ZTE Yuanhang 30 Pro root unlock BL magick ZTE 7532n 8040n 9041n brush mask original brush package root method Download
随机推荐
金太阳开户安全吗?万一免5开户能办理吗?
快速生成ipa包
个人对卷积神经网络的理解
记录Pytorch中的eval()和no_grad()
图片数据不够?我做了一个免费的图像增强软件
让更多港澳青年了解南沙特色文创产品!“南沙麒麟”正式亮相
The 11th China cloud computing standards and Applications Conference | cloud computing national standards and white paper series release, and Huayun data fully participated in the preparation
Generate XML schema from class
Electron installation problems
Share: ZTE Yuanhang 30 Pro root unlock BL magick ZTE 7532n 8040n 9041n brush mask original brush package root method Download
What is the reason why the video cannot be played normally after the easycvr access device turns on the audio?
The easycvr platform reports an error "ID cannot be empty" through the interface editing channel. What is the reason?
《ClickHouse原理解析与应用实践》读书笔记(5)
sample_ What is the relationship between rate, sample and duration
第十一届中国云计算标准和应用大会 | 云计算国家标准及白皮书系列发布 华云数据全面参与编制
Notes on common management commands of openshift
@Extension、@SPI注解原理
Matlab built-in function how different colors, matlab subsection function different colors drawing
【HCIA-cloud】【1】云计算的定义、什么是云计算、云计算的架构与技术说明、华为云计算产品、华为内存DDR配置工具说明
Can communication of nano