Two methods of MySql comma concatenation string query

Two methods of MySql comma concatenation string query

The following two functions are used in the same way as FIND_IN_SET . When using them, you only need to replace FIND_IN_SET FIND_PART_IN_SET or FIND_ALL_PART_IN_SET

For example, a field is 1, 2, 3, 4, 5

Directions:

The first one, pass in 1, 3, 6 to find out

select * from XXX where FIND_PART_IN_SET('1,3,6','1,2,3,4,5')

The second type, passing in 1, 3, 6, cannot be found

select * from XXX where FIND_ALL_PART_IN_SET('1,3,6','1,2,3,4,5')

function:

The first type: as long as one of them is included, it can be detected

CREATE DEFINER = `root`@`%` FUNCTION `NewProc`(str1 text,str2 text)
 RETURNS text
BEGIN
 #Pass in two comma-separated strings and determine whether the second string contains the single character after the split of the first string DECLARE CURRENTINDEX INT; #Current subscript DECLARE CURRENTSTR text;
DECLARE result int;
set result = 0;
set CURRENTINDEX = 0;
set CURRENTSTR = '';
IF str1 IS NOT NULL AND str1 != '' THEN
 SET CURRENTINDEX = LOCATE(',',str1);
 WHILE CURRENTINDEX > 0 DO
 SET CURRENTSTR = substring(str1,1,CURRENTINDEX-1);
 if FIND_IN_SET(CURRENTSTR,str2) THEN
  set result = 1;
 end if;
 SET str1 = substring(str1,CURRENTINDEX+1);
 SET CURRENTINDEX = LOCATE(',',str1);
 END WHILE;
 #Only pass one and no comma at the end IF LENGTH(str1) > 0 THEN
 if FIND_IN_SET(str1,str2) THEN
  set result = 1;
 end if;
 END IF;
END IF;
RETURN result;
END;

The second type: All must be included to be detected

CREATE DEFINER = `root`@`%` FUNCTION `NewProc`(str1 text,str2 text)
 RETURNS text
BEGIN
 #Pass in two comma-separated strings to determine whether the second string contains all the single characters after the split of the first string DECLARE CURRENTINDEX INT; #Current subscript DECLARE CURRENTSTR text;
DECLARE RESULT int;
DECLARE TOTALCOUNT int;
DECLARE TRUECOUNT int;
set RESULT = 0;
set CURRENTINDEX = 0;
set CURRENTSTR = '';
set TOTALCOUNT = 0;
set TRUECOUNT = 0;
IF str1 IS NOT NULL AND str1 != '' THEN
 SET CURRENTINDEX = LOCATE(',',str1);
 WHILE CURRENTINDEX > 0 DO
 SET TOTALCOUNT = TOTALCOUNT + 1;
 SET CURRENTSTR = substring(str1,1,CURRENTINDEX-1);
 if FIND_IN_SET(CURRENTSTR,str2) THEN
  SET TRUECOUNT = TRUECOUNT + 1;
 end if;
 SET str1 = substring(str1,CURRENTINDEX+1);
 SET CURRENTINDEX = LOCATE(',',str1);
 END WHILE;
 #Only pass one and no comma at the end IF LENGTH(str1) > 0 THEN
 SET TOTALCOUNT = TOTALCOUNT + 1;
 if FIND_IN_SET(str1,str2) THEN
  SET TRUECOUNT = TRUECOUNT + 1;
 end if;
 END IF;
END IF;
IF TOTALCOUNT > 0 AND TRUECOUNT = TOTALCOUNT THEN 
 SET RESULT = 1;
END IF;
RETURN result;
END;

Summarize

The above are two methods of MySql comma concatenation string query introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!
If you find this article helpful, please feel free to reprint it and please indicate the source. Thank you!

You may also be interested in:
  • Mysql string interception and obtaining data in the specified string
  • MySQL intercepts the sql statement of the string function
  • MySQL implements string concatenation, interception, replacement, and position search operations

<<:  Vue realizes the whole process of slider drag verification function

>>:  How to use the Linux nl command

Recommend

Detailed tutorial on installing MySQL 8.0 from source code on CentOS 7.4

Table of contents 1. Environment 2. Preparation 3...

Example of cross-database query in MySQL

Preface In MySQL, cross-database queries are main...

Building an image server with FastDFS under Linux

Table of contents Server Planning 1. Install syst...

Modify the jvm encoding problem when Tomcat is running

question: Recently, garbled data appeared when de...

How to use Vue to implement CSS transitions and animations

Table of contents 1. The difference between trans...

Example analysis of mysql shared lock and exclusive lock usage

This article uses examples to illustrate the usag...

Linux Basic Tutorial: Special Permissions SUID, SGID and SBIT

Preface For file or directory permissions in Linu...

Mysql experiment: using explain to analyze the trend of indexes

Overview Indexing is a skill that must be mastere...

translate(-50%,-50%) in CSS achieves horizontal and vertical centering effect

translate(-50%,-50%) attributes: Move it up and l...

Solution to the horizontal scroll bar in iframe under IE6

The situation is as follows: (PS: The red box repr...

Several important MySQL variables

There are many MySQL variables, some of which are...

Will this SQL writing method really cause the index to fail?

Preface There are often some articles on the Inte...

Linux remote control windows system program (three methods)

Sometimes we need to remotely run programs on the...