1. <legend id='wFsqo'><style id='wFsqo'><dir id='wFsqo'><q id='wFsqo'></q></dir></style></legend>

        <bdo id='wFsqo'></bdo><ul id='wFsqo'></ul>
    2. <tfoot id='wFsqo'></tfoot>
    3. <i id='wFsqo'><tr id='wFsqo'><dt id='wFsqo'><q id='wFsqo'><span id='wFsqo'><b id='wFsqo'><form id='wFsqo'><ins id='wFsqo'></ins><ul id='wFsqo'></ul><sub id='wFsqo'></sub></form><legend id='wFsqo'></legend><bdo id='wFsqo'><pre id='wFsqo'><center id='wFsqo'></center></pre></bdo></b><th id='wFsqo'></th></span></q></dt></tr></i><div id='wFsqo'><tfoot id='wFsqo'></tfoot><dl id='wFsqo'><fieldset id='wFsqo'></fieldset></dl></div>

      <small id='wFsqo'></small><noframes id='wFsqo'>

      1. 拆分字符串并循环遍历 MySql 过程中的值

        时间:2023-06-03

          • <bdo id='6WiH3'></bdo><ul id='6WiH3'></ul>

            <i id='6WiH3'><tr id='6WiH3'><dt id='6WiH3'><q id='6WiH3'><span id='6WiH3'><b id='6WiH3'><form id='6WiH3'><ins id='6WiH3'></ins><ul id='6WiH3'></ul><sub id='6WiH3'></sub></form><legend id='6WiH3'></legend><bdo id='6WiH3'><pre id='6WiH3'><center id='6WiH3'></center></pre></bdo></b><th id='6WiH3'></th></span></q></dt></tr></i><div id='6WiH3'><tfoot id='6WiH3'></tfoot><dl id='6WiH3'><fieldset id='6WiH3'></fieldset></dl></div>
            • <tfoot id='6WiH3'></tfoot>
                <legend id='6WiH3'><style id='6WiH3'><dir id='6WiH3'><q id='6WiH3'></q></dir></style></legend>

                  <tbody id='6WiH3'></tbody>

                  <small id='6WiH3'></small><noframes id='6WiH3'>

                  本文介绍了拆分字符串并循环遍历 MySql 过程中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

                  I got a situation where I have to pass a comma separated string to MySQL procedure and split that string and insert those values as rows in to a table.

                  如下图

                  例如,如果我将 'jhon,swetha,sitha' 字符串传递给 mysql 程序,那么它必须用逗号分割该字符串并将这些值作为 3 条记录插入到表中.

                  For example if I passed 'jhon,swetha,sitha' string to mysql procedure then it have to split that string by comma and insert those values as 3 records into a table.

                    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
                  

                  但这并没有解决任何问题.

                  But this does not worked any solution please.

                  推荐答案

                  您需要更加小心地处理字符串.您不能为此使用 REPLACE(),因为这将替换多次出现,如果逗号分隔列表中的一个元素是另一个元素的子字符串,则会损坏您的数据.INSERT() 字符串function 对此更好,不要与用于插入表的 INSERT 语句混淆.

                  You'll need to be a little more careful with your string manipulation. You can't use REPLACE() for this, because that will replace multiple occurrences, corrupting your data if one element in the comma-separated list is a substring of another element. The INSERT() string function is better for this, not to be confused with the INSERT statement used for inserting into a table.

                  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 行影响.

                  Note the "1 row affected" does not mean what you would expect. It refers to the last insert we did. Since we insert one row at a time, if the procedure inserts at least one row, you'll always get a row count of 1; if the procedure inserts nothing, you'll get 0 rows affected.

                  成功了吗?

                  mysql> SELECT * FROM t1;
                  +----+------+
                  | id | c1   |
                  +----+------+
                  |  1 | foo  |
                  |  2 | bar  |
                  |  3 | buzz |
                  |  4 | fizz |
                  +----+------+
                  4 rows in set (0.00 sec)
                  

                  这篇关于拆分字符串并循环遍历 MySql 过程中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在mysql中获得每个对应月份的第一天? 下一篇:使用 Count 查找出现次数

                  相关文章

                  • <bdo id='9qiqR'></bdo><ul id='9qiqR'></ul>

                  1. <small id='9qiqR'></small><noframes id='9qiqR'>

                    <legend id='9qiqR'><style id='9qiqR'><dir id='9qiqR'><q id='9qiqR'></q></dir></style></legend>

                    1. <i id='9qiqR'><tr id='9qiqR'><dt id='9qiqR'><q id='9qiqR'><span id='9qiqR'><b id='9qiqR'><form id='9qiqR'><ins id='9qiqR'></ins><ul id='9qiqR'></ul><sub id='9qiqR'></sub></form><legend id='9qiqR'></legend><bdo id='9qiqR'><pre id='9qiqR'><center id='9qiqR'></center></pre></bdo></b><th id='9qiqR'></th></span></q></dt></tr></i><div id='9qiqR'><tfoot id='9qiqR'></tfoot><dl id='9qiqR'><fieldset id='9qiqR'></fieldset></dl></div>
                      <tfoot id='9qiqR'></tfoot>