程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了拆分字符串并遍历MySql过程中的值大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决拆分字符串并遍历MySql过程中的值?

开发过程中遇到拆分字符串并遍历MySql过程中的值的问题如何解决?下面主要结合日常开发的经验,给出你关于拆分字符串并遍历MySql过程中的值的解决方法建议,希望对你解决拆分字符串并遍历MySql过程中的值有所启发或帮助;

您需要更加谨慎地进行字符串操作。您不能使用replaCE()它,因为如果用逗号分隔的列表中的一个元素是另一元素的子字符串,它将替换多次出现的数据,从而破坏数据。该insert()字符串函数是为了这个美好的,不要与混淆INSERT用于插入到表的语句。@H_502_5@

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 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 = 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 ;

接下来,一个测试表:@H_502_5@

create table `t1` (
  `ID` int(11) NOT NulL auto_INCREMENT,
  `c1` varchar(64) NOT NulL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAulT CHARSET=utf8;

新表为空。@H_502_5@

@H_652_3@mysqL> SELECT * FROM t1;
Empty set (0.00 seC)

调用过程。@H_502_5@

@H_652_3@mysqL> CALL insert_csv('foo,bar,buzz,fizz');
query OK, 1 row affected (0.00 seC)

请注意“受影响的第一行”并不意味着您所期望的。它指的是我们所做的最后一次插入。由于我们一次插入一行,因此,如果该过程 至少 插入一行,那么您将始终获得1的行计数。如果该过程不插入任何内容,则将影响0行。@H_502_5@

奏效了吗?@H_502_5@

@H_652_3@mysqL> SELECT * FROM t1;
+----+------+
| ID | c1   |
+----+------+
|  1 | foo  |
|  2 | bar  |
|  3 | buzz |
|  4 | fizz |
+----+------+
4 rows in set (0.00 seC)

解决方法

我遇到一种情况,我必须将逗号分隔的字符串传递给MySQL过程,然后拆分该字符串,并将这些值作为行插入表中。

如下图所示

例如,如果我将’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@R_932_6758@),'');
         SET str = replace(str,tmp,'');
         //insert tmp into a table.
       END WHILE;
    END

但这请没有任何解决方案。

大佬总结

以上是大佬教程为你收集整理的拆分字符串并遍历MySql过程中的值全部内容,希望文章能够帮你解决拆分字符串并遍历MySql过程中的值所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。