天天看点

mysql 自定义函数【比对字符串,返回差异字符】

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

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      

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

继续阅读