当前位置:网站首页>mysql——find_ in_ Set usage
mysql——find_ in_ Set usage
2022-06-11 23:05:00 【Zhen Jie.】
MySQL In the manual find_in_set Syntax explanation of function :
FIND_IN_SET(str,strlist)
str String to query
strlist Field name Parameter with ”,” Separate Such as (1,2,6,8,10,22)
Query field (strlist) Contained in the (str) Result , The return result is null Or record
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 .
It's ok if you don't understand the concept , Sub category by :
Example 1:
SELECT FIND_IN_SET('b', 'a,b,c,d');
because b stay strlist Put in the collection 2 The location of from 1 Start
select FIND_IN_SET(‘1’, ‘1’); return Namely 1 By this time strlist The set is a little special There is only one string In fact, it requires the previous string Be sure to return greater than... In the latter string collection 0 Number of numbers
select FIND_IN_SET('2', '1,2'); /* return 2*/
select FIND_IN_SET('6', '1'); /* return 0 strlist Does not exist in the str, So back 0.*/
find_in_set() and in The difference between :
Get a test chart to show the difference between the two
CREATE TABLE `tb_test` (
`id` int(8) NOT NULL auto_increment,
`name` varchar(255) NOT NULL,
`list` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
);
INSERT INTO `tb_test` VALUES (1, 'name', 'daodao,xiaohu,xiaoqin');
INSERT INTO `tb_test` VALUES (2, 'name2', 'xiaohu,daodao,xiaoqin');
INSERT INTO `tb_test` VALUES (3, 'name3', 'xiaoqin,daodao,xiaohu');
I thought mysql You can do this query :
SELECT id,name,list from tb_test WHERE 'daodao' IN(list); -- ( One )
In fact, this is not the case , This is only when list The value of the field is equal to ’daodao’ when ( and IN The preceding string exactly matches ), The query is valid , Otherwise, there will be no results , Even if ’daodao’ It's really list in .
Let's take a look at this :
SELECT id,name,list from tb_test WHERE 'daodao' IN ('libk', 'zyfon', 'daodao'); -- ( Two )
This is OK .
What's the difference between these two ? Why can't Article 1 get the right result , And the second one can get results . The reason is ( One ) in (list) list It's a variable. , and ( Two ) in (‘libk’, ‘zyfon’, ‘daodao’) Is a constant .
So if we want to let ( One ) Can work correctly , Need to use
find_in_set():
SELECT id,name,list from tb_test WHERE FIND_IN_SET('daodao',list); -- ( One ) Improved version
summary :
So if list Is a constant , You can use IN, Otherwise, use find_in_set() function .
These are the two sql Yes, the effect of query is the same :
SELECT * from C_PURCHASINGMASTERDATA where FIND_IN_SET(EKGRP,'C54,C02,C14,C60,C06,C61,C53,C51,C12,C08,C03,C07')
SELECT * from C_PURCHASINGMASTERDATA where EKGRP in ('C54','C02','C14','C60','C06','C61','C53','C51','C12','C08','C03','C07')
But if the second sentence sql The values inside are passed in sql A variable field of , So the second sentence sql It's not easy to use . It should be decided on the basis of the actual situation in Or use it find_in_set() function .
find_in_set() and like The difference between :
The main difference is like Is a broad range of fuzzy queries , and find_in_set() It's an exact match , And... Is used between field values ‘,' Separate .
Now I want to query the role number 2 Users of , use like Keyword query :
SELECT userid,username,userrole role FROM `user` WHERE userrole LIKE '%2%';
result :
use find_in_set() Inquire about :
SELECT userid,username,userrole role FROM `user` WHERE find_in_set('2',userrole)
result :
Obviously with find_in_set() The query result is the result we want . So their
The main difference is like Is a broad range of fuzzy queries ; and find_in_set() It's an exact match , And... Is used between field values ‘,' Separate ,Find_IN_SET The result of the query should be less than like Result of query .
mysql in find_in_set() and in() Usage comparison
stay mysql in in Can include a specified number , and find_in_set() For specific data types .
find_in_set Function usage
For example :
There's a list of articles with a type Field , It stores article types , Yes 1 headlines 、2 recommend 、3 hotspot 、4 Image & Text …1,12,13 wait .
Now there's an article that he's both headlines , 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 articles with standard graphics and text ??
That's what we need find_in_set It's time to start .
Here's the quote :
select * from article where FIND_IN_SET('4',type)
MySQL In the manual find_in_set Syntax of functions :
FIND_IN_SET(str,strlist)
If the string str In by N List of strings made up 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 .
mysql> SELECT FIND_IN_SET('b', 'a,b,c,d');
-> 2 because b stay strlist Put in the collection 2 The location of from 1 Start
select FIND_IN_SET(‘1’, ‘1’); return Namely 1 By this time strlist The set is a little special There is only one string In fact, it requires the previous string Be sure to be in the next string set To return to Greater than 0 Number of numbers
select FIND_IN_SET('2', '1,2'); return 2
select FIND_IN_SET('6', '1'); return 0
Be careful :
select * from treenodes where FIND_IN_SET(id, '1,2,3,4,5');
Use find_in_set Function returns multiple records at a time
id Is the field of a table , And then each record is id be equal to 1,2,3,4,5 When
It's kind of similar in ( aggregate )
select * from treenodes where id in (1,2,3,4,5);
Get a test chart to show the difference between the two
CREATE TABLE `test` (
`id` int(8) NOT NULL auto_increment,
`name` varchar(255) NOT NULL,
`list` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
)
INSERT INTO `test` VALUES (1, 'name', 'daodao,www.jb51.net,xiaoqin');
INSERT INTO `test` VALUES (2, 'name2', 'xiaohu,daodao,xiaoqin');
INSERT INTO `test` VALUES (3, 'name3', 'xiaoqin,daodao,www.jb51.net');
I thought MySQL You can do this query :
( One )
select id, list, name from table where 'daodao' IN (list);
In fact, this is not the case , This is only when name yes list When the first element in , The query is valid , Otherwise, there will be no results , Even if ’daodao’ It's really list in .
Let's take a look at this :
( Two )
select id, list, name from table where 'daodao' IN ('libk', 'zyfon', 'daodao');
This is OK .
What's the difference between these two ? Why can't Article 1 get the right result , And the second one can get results .
The reason is ( One ) in (list) list It's a variable. , and ( Two ) in (‘libk’, ‘zyfon’, ‘daodao’) Is a constant .
So if we want to let ( One ) Can work correctly , Need to use find_in_set():
select id, list, name from table where find_in_set('daodao',list);
( One ) Improved version .
summary :
So if list Is a constant , You can use IN, Otherwise, use find_in_set() function .
The above is a brief introduction mysql in find_in_set() Function of the use of detailed explanation , I hope that's helpful , If you have any questions, please leave me a message , Xiaobian will reply to you in time . Thank you very much for your support of script House website !
边栏推荐
- Exercise 11-2 find week (15 points)
- MySQL 8.0 解压版安装教程
- Wireless communication comparison of si4463, si4438 and Si4432 schemes of wireless data transmission module
- Recruitment of audio and video quality test and Development Engineer
- 2022年安全员-B证理论题库及模拟考试
- [Day9 literature extensive reading] on the (a) symmetry between the perception of time and space in large-scale environments
- 帝国理工等最新《胶囊网络综述》论文,29页pdf阐述胶囊的概念、方法与应用
- [day1/5 literature intensive reading] speed constancy or only slowness: what drives the kappa effect
- Point cloud read / write (2): read / write TXT point cloud (space separated | comma separated)
- 926. flip string to monotonic increment
猜你喜欢

Library management system

Summary of personal wrong questions (the wrong questions have not been solved and are used for help)

【Day4 文献精读】Space–time interdependence: Evidence against asymmetric mapping between time and space
![[day13-14 intensive literature reading] cross dimensional magnetic interactions arise from memory interference](/img/e0/94602f0b7b6e50f55e29b6147a9df2.png)
[day13-14 intensive literature reading] cross dimensional magnetic interactions arise from memory interference
![[Day9 literature extensive reading] on the (a) symmetry between the perception of time and space in large-scale environments](/img/06/3011aef2e9e26cbc7c63b5c2967df7.png)
[Day9 literature extensive reading] on the (a) symmetry between the perception of time and space in large-scale environments

Unity3d C#开发微信小游戏音频/音效播放问题解决过程分享

Cloudcompare source code analysis: read ply file

Zigbee3.0 wireless packet capturing installation method based on e18-2g4u04b

Computer forced shutdown Oracle login failed
![[day11-12 intensive literature reading] on languages in memory: an internal clock account of space-time interaction](/img/85/4486bd46b5f32331ce398e42e5d803.png)
[day11-12 intensive literature reading] on languages in memory: an internal clock account of space-time interaction
随机推荐
遇到表格,手动翻页太麻烦?我教你写脚本,一页展示所有数据
Teacher lihongyi, NTU -- tips for DNN regulation
Processus postgresql10
Research Report on development trend and competitive strategy of global seabed leakage detection system industry
Four rounding modes in IEEE754 standard
[Day2 intensive literature reading] time in the mind: using space to think about time
Analysis on the market prospect of smart home based on ZigBee protocol wireless module
JsonParseException: Unrecognized token ‘username‘: was expecting表单提交登陆数据报错
Summary of personal wrong questions (the wrong questions have not been solved and are used for help)
16 | 浮点数和定点数(下):深入理解浮点数到底有什么用?
Gcache of goframe memory cache
Number of classified statistical characters (15 points)
5. Xuecheng project Alipay payment
6. project launch
2022年R1快开门式压力容器操作考题及在线模拟考试
Research Report on development trend and competitive strategy of global metallized high barrier film industry
SecurityContextHolder.getContext().getAuthentication().getPrincipal()获取到的是username而不是UserDetails
Deconstruction of volatile | community essay solicitation
【Day2 文献精读】Time in the mind: Using space to think about time
What are the pitfalls of redis's current network: using a cache and paying for disk failures?