当前位置:网站首页>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'; |
边栏推荐
- Modify the original place where the method needs to be called and triggered
- Stm32g474 infrared receiving based on irtim peripherals
- Are cloud game servers expensive to rent? Factors to consider in cloud game server leasing
- Must the company domain name have a trademark registration? What if the registered domain name is rejected?
- Tencent Ding Ke: the mission of Tencent security is to safeguard the beauty of digital
- If there are enumerations in the entity object, the conversion of enumerations can be carried out with @jsonvalue and @enumvalue annotations
- Live broadcast of the double 11 King bombing! Must buy good things introduction, come on~
- WordPress site quickly integrates Tencent's digital identity management and control platform CIAM to realize login authentication without development
- Kubesphere upgrade & enable plug-ins after installation
- Using nginscript as a file distribution service with permission
猜你喜欢

Advanced BOM tool intelligent packaging function

163 mailbox login portal display, enterprise mailbox computer version login portal

BIM model example

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

Stm32g474 infrared receiving based on irtim peripherals

application. Yaml configuring multiple running environments

Introduction to development model + test model

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

layer 3 switch

2020 language and intelligent technology competition was launched, and Baidu provided the largest Chinese data set
随机推荐
Dry goods collection | the heaviest content collection of Tencent digital ecology Conference!
Live broadcast of the double 11 King bombing! Must buy good things introduction, come on~
How to batch output ean 13 code to pictures
Case of data recovery by misoperation under NTFS file system
Leetcode problem solving notes for slow ploughing of stupid cattle (dynamic update...)
[supply chain • case] Tianneng group: the key to understanding the leading battery manufacturer to achieve the first profit fault
How to obtain the information of easydss single / multiple live streams through the API interface?
[seckill] new / old users lock in the "explosive seckill zone" and snap up the one-year validity cloud function resource yyds for 63 yuan!
Facebook was officially renamed meta, and the digital twin meta universe was applied!
Comparison of four dynamic routes: Rip, OSPF, Isis, BGP
The whole procurement process of the procurement mall website in the transportation industry is electronic to reduce the procurement cost
What is the reason why the switching page group disappears after easycvr establishes a multi-level group?
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?
Centeros environment setup
The new purchased machines with large customized images are slow to enter the system
How to build a website? What needs attention?
WordPress site quickly integrates Tencent's digital identity management and control platform CIAM to realize login authentication without development
A review of Nature Neuroscience: dynamic representation in networked nervous system
2020 language and intelligent technology competition was launched, and Baidu provided the largest Chinese data set
How to use the cloud game server is the cloud server stable