天天看點

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      

原理很簡單:通過特殊字元“,”,循環截取第一個字元進行比對,傳回不存在于另一個字元的部分。

繼續閱讀