当前位置:网站首页>Mysql Find_ IN_ Set function
Mysql Find_ IN_ Set function
2022-06-24 02:18:00 【Dong Dong Dong Dong Dong Dong Dong】
While working on the project today , I saw one I've never seen before MySQL function ——FIND_IN_SET(), There was a strong interest immediately , And then I searched , Turn over .
grammar :FIND_IN_SET(str,strlist)
Definition :
1. If character string str In by N Composed of sub chains String list strlist in , Then the return value range is 1 To N Between .
2. A list of strings is a list of characters that are ‘,’ A string of symbols separated from a chain .
3. If the first parameter is a constant string , And the second is typeSET Column , be FIND_IN_SET() Functions are optimized , Use The bit Calculation .
4. If str be not in strlist or strlist Is an empty string , The return value is 0.
5. If any parameter is NULL, The return value is NULL. This function contains a comma in the first argument (‘,’) Will not work properly .
strlist: An English comma “,” Linked string , for example :"a,b,c,d", The string is similar in form to SET The values of types are linked by commas .
notes : The fields must be separated by commas , Tested by bloggers in person , Fields separated by Chinese commas will not be searched , Even if the field contains the string to search
Example :SELECT FIND_IN_SET('b','a,b,c,d'); // The return value is 2, That is to say 2 It's worth
mysql Medium IN and FIND_IN_SET Query questions for
I thought mysql You can do this query
select id, list, name from table where 'daodao' IN (list); ( One )
notes :1. table There are three fields id:int, list:varchar(255), name:varchar(255)
In fact, this is not the case , This is only when 'daodao' yes list The first element in ( When I tested it, it seemed that I was the first one, but I couldn't either , Only when list The value of the field is equal to daodao That's right ) when , The query is valid , Otherwise, none of them will come to an end , Even if 'daodao' It's really list in
Test code :
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,xiaohu,xiaoqin');
INSERT INTO `test` VALUES (2, 'name2', 'xiaohu,daodao,xiaoqin');
INSERT INTO `test` VALUES (3, 'name3', 'xiaoqin,daodao,xiaohu');
test1:sql = select * from `test` where 'daodao' IN (`list`);
Get result null .
test2:sql = select * from `test` where FIND_IN_SET('daodao',`list`);
Get three pieces of data .
1 | name | daodao,xiaohu,xiaoqin | |||
2 | name2 | xiaohu,daodao,xiaoqin | |||
3 | name3 | xiaoqin,daodao,xiaohu |
Modify table data
update `test` set `list`='daodao' where `id`='1';
And then execute test1 Of sql, You can return a result .
Let's take a look at this :
select id, list, name from table 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, 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
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 of symbols separated from a chain . 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 Extend usage , utilize FIND_IN_set Sort eg: $ids = '9,3,45,1,8,2,6'; |
边栏推荐
- Evaluation index of machine learning model
- An attempt to use Navicat tool to copy and export MySQL database data
- Centeros environment setup
- Wwise + GME game voice scheme: unlock more voice playing methods and let players "sound in their environment"
- Cloud recommendation Vol.1: quick start to remote control under f-stack, go modules and 5g!
- IP address and subnet partition are very important. This article is very fragrant!
- Development status of industrial Internet
- What is a region name? Can a territory name be used for trademark registration?
- Tencent peace of mind ecological alliance was officially established as a linkage partner. Open technology helps "digital agriculture"
- Must the company domain name have a trademark registration? What if the registered domain name is rejected?
猜你喜欢

application. Yaml configuring multiple running environments

Advanced BOM tool intelligent packaging function

2020 language and intelligent technology competition was launched, and Baidu provided the largest Chinese data set

If there are enumerations in the entity object, the conversion of enumerations can be carried out with @jsonvalue and @enumvalue annotations

Review of AI hotspots this week: the Gan compression method consumes less than 1/9 of the computing power, and the open source generator turns your photos into hand drawn photos

BIM model example

Stm32g474 infrared receiving based on irtim peripherals

Leetcode969: pancake sorting (medium, dynamic programming)

How to fill in and register e-mail, and open mass mailing software for free

Introduction to development model + test model
随机推荐
The cloud University of "digital and real integration, CO building authentic Internet" is surging forward
Gin framework: adding tracing Middleware
Leetcode problem solving notes for slow ploughing of stupid cattle (dynamic update...)
Benchmarking Shopify? Similarities and differences between "two giants" of Chinese e-commerce SAAS and Weimeng
UNIX command encyclopedia, common commands are here, work must!
Offline store + online mall, why do you want to be an online mall
2021-11-10:o (1) time inserts, deletes and obtains random elements. Implement ra
Using robot framework to realize multi platform automated testing
Why do cloud desktops use rack servers? What are the functions of cloud desktop?
A complete collection of SQL commands. Each command has an example. Xiaobai can become a God after reading it!
Global and Chinese dealox industry development status and demand trend forecast report 2022-2028
Is it illegal to use someone else's trademark to register a domain name? What should we pay attention to when registering a domain name?
The difference between classless routing and classless routing
Looking at the future development direction of industrial Internet from the transformation of circulation links of large building materials
Vivo global mall: design and practice of commodity system architecture
Based on ARM embedded real-time streaming media service development and deployment, easygbs supports arm64 architecture
Facebook was officially renamed meta, and the digital twin meta universe was applied!
Flowable get the input stream of the current task process picture
Stm32g474 infrared receiving based on irtim peripherals
Using nginscript as a file distribution service with permission