在MySql过程中拆分字符串并循环值
在这种情况下,我必须将逗号分隔的字符串传递给MySQL过程,并拆分该字符串,然后将这些值作为行插入表中在MySql过程中拆分字符串并循环值,mysql,sql,stored-procedures,Mysql,Sql,Stored Procedures,在这种情况下,我必须将逗号分隔的字符串传递给MySQL过程,并拆分该字符串,然后将这些值作为行插入表中 CREATE PROCEDURE new_routine (IN str varchar(30)) BEGIN DECLARE tmp varchar(10); DECLARE inc INT DEFAULT 0; WHILE INSTR(str, ',') DO SET tmp = SUBSTRING(SUBSTR
CREATE PROCEDURE new_routine (IN str varchar(30))
BEGIN
DECLARE tmp varchar(10);
DECLARE inc INT DEFAULT 0;
WHILE INSTR(str, ',') DO
SET tmp = SUBSTRING(SUBSTRING_INDEX(str,',',inc),LENGTH(SUBSTRING_INDEX(str,',',inc-1))+1),',','');
SET str = REPLACE(str, tmp, '');
//insert tmp into a table.
END WHILE;
END
DELIMITER $$
DROP PROCEDURE IF EXISTS `insert_csv` $$
CREATE PROCEDURE `insert_csv`(_list MEDIUMTEXT)
BEGIN
DECLARE _next TEXT DEFAULT NULL;
DECLARE _nextlen INT DEFAULT NULL;
DECLARE _value TEXT DEFAULT NULL;
iterator:
LOOP
-- exit the loop if the list seems empty or was null;
-- this extra caution is necessary to avoid an endless loop in the proc.
IF CHAR_LENGTH(TRIM(_list)) = 0 OR _list IS NULL THEN
LEAVE iterator;
END IF;
-- capture the next value from the list
SET _next = SUBSTRING_INDEX(_list,',',1);
-- save the length of the captured value; we will need to remove this
-- many characters + 1 from the beginning of the string
-- before the next iteration
SET _nextlen = CHAR_LENGTH(_next);
-- trim the value of leading and trailing spaces, in case of sloppy CSV strings
SET _value = TRIM(_next);
-- insert the extracted value into the target table
INSERT INTO t1 (c1) VALUES (_value);
-- rewrite the original string using the `INSERT()` string function,
-- args are original string, start position, how many characters to remove,
-- and what to "insert" in their place (in this case, we "insert"
-- an empty string, which removes _nextlen + 1 characters)
SET _list = INSERT(_list,1,_nextlen + 1,'');
END LOOP;
END $$
DELIMITER ;
如下所示
例如,如果我将'jhon,swetha,sitha'字符串传递给mysql过程,那么它必须用逗号分割该字符串,并将这些值作为3条记录插入表中
CREATE PROCEDURE new_routine (IN str varchar(30))
BEGIN
DECLARE tmp varchar(10);
DECLARE inc INT DEFAULT 0;
WHILE INSTR(str, ',') DO
SET tmp = SUBSTRING(SUBSTRING_INDEX(str,',',inc),LENGTH(SUBSTRING_INDEX(str,',',inc-1))+1),',','');
SET str = REPLACE(str, tmp, '');
//insert tmp into a table.
END WHILE;
END
DELIMITER $$
DROP PROCEDURE IF EXISTS `insert_csv` $$
CREATE PROCEDURE `insert_csv`(_list MEDIUMTEXT)
BEGIN
DECLARE _next TEXT DEFAULT NULL;
DECLARE _nextlen INT DEFAULT NULL;
DECLARE _value TEXT DEFAULT NULL;
iterator:
LOOP
-- exit the loop if the list seems empty or was null;
-- this extra caution is necessary to avoid an endless loop in the proc.
IF CHAR_LENGTH(TRIM(_list)) = 0 OR _list IS NULL THEN
LEAVE iterator;
END IF;
-- capture the next value from the list
SET _next = SUBSTRING_INDEX(_list,',',1);
-- save the length of the captured value; we will need to remove this
-- many characters + 1 from the beginning of the string
-- before the next iteration
SET _nextlen = CHAR_LENGTH(_next);
-- trim the value of leading and trailing spaces, in case of sloppy CSV strings
SET _value = TRIM(_next);
-- insert the extracted value into the target table
INSERT INTO t1 (c1) VALUES (_value);
-- rewrite the original string using the `INSERT()` string function,
-- args are original string, start position, how many characters to remove,
-- and what to "insert" in their place (in this case, we "insert"
-- an empty string, which removes _nextlen + 1 characters)
SET _list = INSERT(_list,1,_nextlen + 1,'');
END LOOP;
END $$
DELIMITER ;
但这并不能解决任何问题。您需要更加小心地处理字符串。不能对此使用
REPLACE()
,因为这将替换多次出现,如果逗号分隔列表中的一个元素是另一个元素的子字符串,则会损坏数据。最好不要与用于插入到表中的INSERT
语句混淆
CREATE PROCEDURE new_routine (IN str varchar(30))
BEGIN
DECLARE tmp varchar(10);
DECLARE inc INT DEFAULT 0;
WHILE INSTR(str, ',') DO
SET tmp = SUBSTRING(SUBSTRING_INDEX(str,',',inc),LENGTH(SUBSTRING_INDEX(str,',',inc-1))+1),',','');
SET str = REPLACE(str, tmp, '');
//insert tmp into a table.
END WHILE;
END
DELIMITER $$
DROP PROCEDURE IF EXISTS `insert_csv` $$
CREATE PROCEDURE `insert_csv`(_list MEDIUMTEXT)
BEGIN
DECLARE _next TEXT DEFAULT NULL;
DECLARE _nextlen INT DEFAULT NULL;
DECLARE _value TEXT DEFAULT NULL;
iterator:
LOOP
-- exit the loop if the list seems empty or was null;
-- this extra caution is necessary to avoid an endless loop in the proc.
IF CHAR_LENGTH(TRIM(_list)) = 0 OR _list IS NULL THEN
LEAVE iterator;
END IF;
-- capture the next value from the list
SET _next = SUBSTRING_INDEX(_list,',',1);
-- save the length of the captured value; we will need to remove this
-- many characters + 1 from the beginning of the string
-- before the next iteration
SET _nextlen = CHAR_LENGTH(_next);
-- trim the value of leading and trailing spaces, in case of sloppy CSV strings
SET _value = TRIM(_next);
-- insert the extracted value into the target table
INSERT INTO t1 (c1) VALUES (_value);
-- rewrite the original string using the `INSERT()` string function,
-- args are original string, start position, how many characters to remove,
-- and what to "insert" in their place (in this case, we "insert"
-- an empty string, which removes _nextlen + 1 characters)
SET _list = INSERT(_list,1,_nextlen + 1,'');
END LOOP;
END $$
DELIMITER ;
接下来是一个测试表:
CREATE TABLE `t1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`c1` varchar(64) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
新桌子是空的
mysql> SELECT * FROM t1;
Empty set (0.00 sec)
调用程序
mysql> CALL insert_csv('foo,bar,buzz,fizz');
Query OK, 1 row affected (0.00 sec)
请注意,“1行受影响”并不是您所期望的。它指的是我们做的最后一次插入。由于我们一次插入一行,因此如果过程至少插入一行,则行数始终为1;如果该过程不插入任何内容,则将影响0行
它起作用了吗
mysql> SELECT * FROM t1;
+----+------+
| id | c1 |
+----+------+
| 1 | foo |
| 2 | bar |
| 3 | buzz |
| 4 | fizz |
+----+------+
4 rows in set (0.00 sec)
非常有用的答案。Greate过程代码,但我认为应该使用
CHAR\u LENGTH
,而不是LENGTH
——否则根据字符集编码,字符串重新计算将是错误的,就像我使用西里尔字符的情况一样。更多信息在这个答案@KiaKaha你是对的,原始版本只使用单字节字符。谢谢你的编辑。