日期:2014-05-16 浏览次数:20842 次
select replace(concat('insert into t2 values(@id,\'',replace(c2,',','\'),(@id,\''),'\')'),'@id',c1) from t1;
------解决方案--------------------
推测楼主想要这样一个函数。居家旅行常备无患。
mysql> DELIMITER $$ mysql> DROP PROCEDURE IF EXISTS `split`$$ Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> CREATE PROCEDURE `split`( -> str VARCHAR(9999), -> sep CHAR(1) -> ) -> BEGIN -> DECLARE strlen INT; -> DECLARE last_index INT; -> DECLARE cur_index INT; -> DECLARE cur_char VARCHAR(200); -> DECLARE len INT; -> SET cur_index=1; -> SET last_index=0; -> SET strlen=LENGTH(str); -> DROP TABLE IF EXISTS tmp_tb_split; -> CREATE TEMPORARY TABLE tmp_tb_split( -> id INT AUTO_INCREMENT, -> VALUE VARCHAR(20), -> PRIMARY KEY (`ID`), -> UNIQUE KEY `ID` (`ID`) -> ) ; -> WHILE(cur_index<=strlen) DO -> BEGIN -> IF SUBSTRING(str FROM cur_index FOR 1)=sep OR cur_index=strlen TH EN -> SET len=cur_index-last_index-1; -> IF cur_index=strlen THEN -> SET len=len+1; -> END IF; -> INSERT INTO tmp_tb_split(`value`)VALUES(SUBSTRING(str FROM (l ast_index+1) FOR len)); -> SET last_index=cur_index; -> END IF; -> SET cur_index=cur_index+1; -> END; -> END WHILE; -> -> SELECT * FROM tmp_tb_split; -> END$$ Query OK, 0 rows affected (0.01 sec) mysql> DELIMITER ; mysql> CALL split('a,b,c',','); +----+-------+ | id | VALUE | +----+-------+ | 1 | a | | 2 | b | | 3 | c | +----+-------+ 3 rows in set (0.23 sec) Query OK, 0 rows affected, 1 warning (0.23 sec) mysql> mysql> mysql> DELIMITER $$ mysql> mysql> DROP PROCEDURE IF EXISTS `split`$$ Query OK, 0 rows affected (0.00 sec) mysql> mysql> CREATE PROCEDURE `split`( -> str VARCHAR(9999), -> sep CHAR(1) -> ) -> BEGIN -> DECLARE strlen INT; -> DECLARE last_index INT; -> DECLARE cur_index INT; -> DECLARE cur_char VARCHAR(200); -> DECLARE len INT; -> SET cur_index=1; -> SET last_index=0; -> SET strlen=LENGTH(str); -> DROP TABLE IF EXISTS tmp_tb_split; -> CREATE TEMPORARY TABLE tmp_tb_split( -> id INT AUTO_INCREMENT, -> VALUE VARCHAR(20), -> PRIMARY KEY (`ID`), -> UNIQUE KEY `ID` (`ID`) -> ) ; -> WHILE(cur_index<=strlen) DO -> BEGIN -> IF SUBSTRING(str FROM cur_index FOR 1)=sep OR cur_index=strlen TH EN -> SET len=cur_index-last_index-1; -> IF cur_index=strlen THEN -> SET len=len+1; -> END IF; -> INSERT INTO tmp_tb_split(`value`)VALUES(SUBSTRING(str FROM (l ast_index+1) FOR len)); -> SET last_index=cur_index; -> END IF; -> SET cur_index=cur_index+1; -> END; -> END WHILE