当前位置:网站首页>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 !
边栏推荐
- Zigbee3.0 wireless packet capturing installation method based on e18-2g4u04b
- [Day8 literature extensive reading] space and time in the child's mind: evidence for a cross dimensional symmetry
- 帝国理工等最新《胶囊网络综述》论文,29页pdf阐述胶囊的概念、方法与应用
- Review C language I
- Svn deploys servers and cleints locally and uses alicloud disks for automatic backup
- Exercise 6-2 using functions to sum a special series of a numbers (20 points)
- PHP+MYSQL图书管理系统(课设)
- Huawei equipment configuration hovpn
- Library management system
- How to make scripts executable anywhere
猜你喜欢

【Day4 文献精读】Space–time interdependence: Evidence against asymmetric mapping between time and space

Mobile terminal - picture timeline of swipe effect

遇到表格,手动翻页太麻烦?我教你写脚本,一页展示所有数据

2022年安全员-A证考题模拟考试平台操作
![[day4 literature intensive reading] space – time interdependence: evidence against Asymmetric mapping between time and space](/img/ce/f3817690a024cfebcf58a5ccc3cfdc.png)
[day4 literature intensive reading] space – time interdependence: evidence against Asymmetric mapping between time and space

通用树形结构的迭代与组合模式实现方案

2022安全员-C证判断题模拟考试平台操作

2022年高处安装、维护、拆除操作证考试题库模拟考试平台操作

MySQL 8.0 解压版安装教程

Bit operation in leetcode
随机推荐
2022年安全员-B证理论题库及模拟考试
Analysis on the market prospect of smart home based on ZigBee protocol wireless module
动态规划之0-1背包问题(详解+分析+原码)
5. Xuecheng project Alipay payment
Exercise 9-1 time conversion (15 points)
Unity3d C#开发微信小游戏音频/音效播放问题解决过程分享
【Day4 文献精读】Space–time interdependence: Evidence against asymmetric mapping between time and space
16 | 浮点数和定点数(下):深入理解浮点数到底有什么用?
H.265编码原理入门
Cloudcompare source code analysis: read ply file
想做钢铁侠?听说很多大佬都是用它入门的
2022安全员-C证判断题模拟考试平台操作
The key to the safe was inserted into the door, and the college students stole the mobile phone numbers of 1.1 billion users of Taobao alone
Jsonparseexception: unrecognized token 'username': was expecting error when submitting login data
队列(C语言)
Matlab point cloud processing (XXV): point cloud generation DEM (pc2dem)
Point cloud read / write (2): read / write TXT point cloud (space separated | comma separated)
通用树形结构的迭代与组合模式实现方案
【Day11-12 文献精读】On magnitudes in memory: An internal clock account of space-time interaction
[day13-14 intensive literature reading] cross dimensional magnetic interactions arise from memory interference