读书人

mysql存储过程瓜分字符串

发布时间: 2012-10-30 16:13:36 作者: rapoo

mysql存储过程分割字符串

DROP PROCEDURE IF EXISTS split_string;CREATE PROCEDURE split_string(IN to_split VARCHAR(255), IN split_with VARCHAR(100))BEGIN  DECLARE total_length INT;  DECLARE location INT;  DROP TEMPORARY TABLE IF EXISTS temp_store;  CREATE TEMPORARY TABLE temp_store(str VARCHAR(100));  WHILE LENGTH(to_split) > 0  DO    SET total_length = LENGTH(to_split);    SET location     = LOCATE(split_with, to_split);    IF location = 0 THEN      INSERT INTO temp_store(str) VALUE(to_split);      SET to_split = '';    ELSE         INSERT INTO temp_store(str) VALUE(LEFT(to_split, location-1));      SET to_split = RIGHT(to_split, total_length-location);    END IF;    END WHILE;  SELECT * FROM temp_store;end

读书人网 >Mysql

热点推荐