DELIMITER $$sql
DROP PROCEDURE IF EXISTS sp_split_str $$ide
CREATE PROCEDURE sp_split_str(字符串
IN str VARCHAR(1000),it
IN split VARCHAR(2)class
)循環
BEGINdi
DECLARE sub_str VARCHAR(1000);view
DECLARE pos INT(11);vi
DROP TEMPORARY TABLE IF EXISTS temp_str;co
CREATE TEMPORARY TABLE temp_str(
str VARCHAR(100)
)ENGINE = INNODB DEFAULT CHARSET=utf8;
SELECT LOCATE(split,str) INTO pos;
WHILE pos > 0 DO
BEGIN
-- 把','左邊的字符串放到臨時表中;
INSERT INTO temp_str(str) VALUES (LEFT(str,pos-1));
-- 把','右邊的字符串從新賦值給str;
SELECT RIGHT(str,CHAR_LENGTH(str)-pos) INTO str;
-- 更新pos的值,進行下一次循環.
SELECT LOCATE(split,str) INTO pos;
END;
END WHILE;
-- 把最後一個字符串放到臨時表中.
INSERT INTO temp_str(str) VALUES (str);
SELECT * FROM temp_str;
DROP TEMPORARY TABLE IF EXISTS temp_str;
END $$
DELIMITER ;