MYSQL存储过程 -- 随笔
分隔字符串:
CREATE DEFINER = root `@`%` FUNCTION `fun_string_split`(IN_IDS longText,IN_FIELD VARCHAR(64),IN_SPLIT VARCHAR(2))
RETERNS LONGTEXT
BEGIN
DECLARE OUT_STRING LONGTEXT;
DECLARE TMP_NEW_STRING LONGTEXT;
SET TMP_FIND_POS = INSTR(IN_IDS,IN_SPLIT);
IF(TMP_FIND_POS > 0)
THEN
SET OUT_STRING = CONCAT(IN_FIELD,'=''');
SET TMP_NEW_STRING = IN_IDS;
WHILE(TMP_FIND_POS > 0) DO
SET TMP_NEW_STRING = INSERT(TMP_NEW_STRING,TMP_FIND_POS,LENGTH(IN_SPLIT),CONCAT(''' OR',IN_FIELD,'='''));
SET TMP_FIND_POS = INSTR(TMP_NEW_STRING,IN_SPLIT);
END WHILE;
SET OUT_STRING = CONCAT(OUT_STRING,TMP_NEW_STRING,'''');
ELSE
SET OUT_STRING = CONCAT(IN_FIELD,'=''',IN_IDS,'''');
END IF;
RETURN OU_STRING;
END