不多哔哔,直接上干货(请无视命名规则)

CREATE FUNCTION `f_str_diff`(guize VARCHAR(255),tongguo VARCHAR(255)) RETURNS varchar(255) CHARSET utf8
    DETERMINISTIC
begin
    DECLARE newName VARCHAR(255);
    DECLARE newName1  VARCHAR(255)  DEFAULT '';
      declare itemindex int(10);
    set guize=concat(guize,',');
    set itemindex=instr(guize,',');
    WHILE(guize!=','and itemindex>0) DO
    set newName = left(guize,itemindex-1);
      IF(instr(tongguo,newName)=0) 
    THEN 
        set newName1 = concat(newName,',',newName1);
      end IF;
      set guize=right(guize,CHAR_LENGTH(guize)-itemindex); 
      set itemindex=instr(guize,',');
    END WHILE;
    RETURN newName1;
   end

原理很简单:通过特殊字符“,”,循环截取第一个字符进行比对,返回不存在于另一个字符的部分。

 

 

网上没找到相关的代码(可能搜索关键词不对吧,哈哈),就自己研究自定义函数,等同于盲写代码,过程中踩了无数的坑,记录一下。