MySQL : Create a function to compare the two conman separated values

CREATE FUNCTION `FIND_IN_TWO_SET`(val TEXT, val2 TEXT) RETURNS int(11)
BEGIN
        DECLARE output TEXT;
        DECLARE inc INT;
        DECLARE mach INT;
        SET inc = 0;
        SET mach = 0;
        
        label1: LOOP
            SET inc = inc + 1;
            SET output = REPLACE(SUBSTRING(SUBSTRING_INDEX(val, ',', inc), CHAR_LENGTH(SUBSTRING_INDEX(val, ',', inc - 1)) + 1), ',', '');
            IF output = '' THEN
                SET output = null;
                LEAVE label1;
            END IF;
            if find_in_set(output, val2) > 0 THEN
                SET mach = 1;
                LEAVE label1;
            END IF;
                ITERATE label1;
            END LOOP label1;
        RETURN mach;
    END

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s