MySQL function to determine whether there is an intersection between two comma-separated strings

 

[mysql] MySQL function to determine whether there is an intersection between two comma-separated strings

www.MyException.Cn User shared on: 2015-07-28 Browse: 0 times
 
[mysql] MySQL function to determine whether there is an intersection between two comma-separated strings

    Suppose I have two strings str1: '1001,1002,1003,1004', str2: '1001,2001,3001,4001'.

 

    Now you need to determine whether there is an intersection between these two comma-separated strings, you can create a mysql function to achieve it.

 

    code show as below: 

#Create FUNCTION
DELIMITER $$;
 
CREATE FUNCTION `is_mixed`(str1 TEXT, str2 TEXT) RETURNS TINYINT
    BEGIN
    DECLARE ismixed TINYINT DEFAULT 0;
    set ismixed:=(select concat(str1, ',') regexp concat(replace(str2,',',',|'), ','));
    RETURN ismixed;
    END;$$
 
DELIMITER ;
 
#Execute FUNCTION
select is_mixed('1001,1002,1003,1004', '1001,2001,3001,4001');

 

    Since MySQL does not have a boolean type, 1 and 0 of the tinyint type are chosen to represent true and false.

 

    Execution result: The return result is 1, indicating true, and there is an intersection.

 

 

    Suppose I have two strings str1: '1001,1002,1003,1004', str2: '1001,2001,3001,4001'.

 

    Now you need to determine whether there is an intersection between these two comma-separated strings, you can create a mysql function to achieve it.

 

    code show as below: 

#Create FUNCTION
DELIMITER $$;
 
CREATE FUNCTION `is_mixed`(str1 TEXT, str2 TEXT) RETURNS TINYINT
    BEGIN
    DECLARE ismixed TINYINT DEFAULT 0;
    set ismixed:=(select concat(str1, ',') regexp concat(replace(str2,',',',|'), ','));
    RETURN ismixed;
    END;$$
 
DELIMITER ;
 
#Execute FUNCTION
select is_mixed('1001,1002,1003,1004', '1001,2001,3001,4001');

 

    Since MySQL does not have a boolean type, 1 and 0 of the tinyint type are chosen to represent true and false.

 

    Execution result: The return result is 1, indicating true, and there is an intersection.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326933262&siteId=291194637