当前位置:网站首页>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 ) 
边栏推荐
- jdbc读大量数据导致内存溢出
- buuctf-pwn write-ups (9)
- Memory leak of viewpager + recyclerview
- Take a look at semaphore, the current limiting tool provided by JUC
- [paddlepaddle] paddedetection face recognition custom data set
- Nacos distributed transactions Seata * * install JDK on Linux, mysql5.7 start Nacos configure ideal call interface coordination (nanny level detail tutorial)
- [QNX Hypervisor 2.2用户手册]6.3.2 配置VM
- [utiliser Electron pour développer le Bureau sur youkirin devrait]
- Gimp 2.10 tutorial "suggestions collection"
- Record eval() and no in pytoch_ grad()
猜你喜欢

A2L file parsing based on CAN bus (3)

解决 contents have differences only in line separators

《2022中国信创生态市场研究及选型评估报告》发布 华云数据入选信创IT基础设施主流厂商!
![Maximum artificial island [how to make all nodes of a connected component record the total number of nodes? + number the connected component]](/img/8b/a60fc36115580f018445e4c2a28a9d.png)
Maximum artificial island [how to make all nodes of a connected component record the total number of nodes? + number the connected component]

Introduction to the development function of Hanlin Youshang system of Hansheng Youpin app

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!

【HCIA-cloud】【1】云计算的定义、什么是云计算、云计算的架构与技术说明、华为云计算产品、华为内存DDR配置工具说明

爬虫01-爬虫基本原理讲解

技术分享 | 常见接口协议解析

使用JMeter录制脚本并调试
随机推荐
如何获取飞机穿过雷达两端的坐标
A2L file parsing based on CAN bus (3)
技术分享 | 常见接口协议解析
Is it safe to make fund fixed investment on access letter?
让更多港澳青年了解南沙特色文创产品!“南沙麒麟”正式亮相
Wu Enda team 2022 machine learning course, coming
Sibling components carry out value transfer (there is a sequence displayed)
How to choose the most formal and safe external futures platform?
Electron installation problems
图片数据不够?我做了一个免费的图像增强软件
Record a case of using WinDbg to analyze memory "leakage"
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
RPC协议详解
How to automatically install pythn third-party libraries
瞅一瞅JUC提供的限流工具Semaphore
Vulnhub's darkhole_ two
sample_rate(采样率),sample(采样),duration(时长)是什么关系
深入底层C源码讲透Redis核心设计原理
sample_ What is the relationship between rate, sample and duration
Is it safe for golden sun to open an account? Can I open an account free of 5 in case?