拆分逗号分隔的字符串MySql

时间:2014-05-22 10:34:47

标签: mysql

表1

+-------+------------------------+
| ename | work_place             |
+-------+------------------------+
| john  | CA,philadelphia        |
| carl  | New Jersey,oklahoma    |
| paul  | New Jersey,oklahoma,CA |
+-------+------------------------+

以下是我正在使用的代码: (我已尝试在代码中混淆条件的顺序,但没有用)

DROP PROCEDURE IF EXISTS `test2`$$
CREATE PROCEDURE `test2`()
BEGIN
DECLARE i INT DEFAULT 0;
DECLARE j INT DEFAULT 0;
DECLARE cnt INT DEFAULT 0;
DECLARE str_len INT;
DECLARE temp_str text DEFAULT '';
DECLARE tr_name varchar(255) DEFAULT '';
DECLARE tr_wp varchar(255) DEFAULT '';
DECLARE exp_cur CURSOR FOR Select ename,work_place from table1;

DROP Temporary Table if exists temp_explode_string;
CREATE Temporary Table  temp_explode_string (emp_name varchar(11), wp varchar(55)) ;

Select count(*) into @name_cnt from table1;

open exp_cur;

get_wp: LOOP

fetch  next from exp_cur into tr_name,tr_wp;
set j=j+1;

SET str_len=LENGTH(tr_wp);  
SET i = round((LENGTH(tr_wp)-LENGTH(REPLACE(tr_wp, ',', '')))/LENGTH(',') + 1); 

WHILE(cnt < i) DO
SET cnt=cnt+1;
SET temp_str = REPLACE(SUBSTRING(SUBSTRING_INDEX(tr_wp, ',', cnt), LENGTH(SUBSTRING_INDEX(tr_wp, ',',cnt - 1)) + 1), ',', '');
Insert into temp_explode_string values (tr_name,temp_str);  
END WHILE;


IF j=@name_cnt THEN 
            LEAVE get_wp;
        END IF;

END LOOP get_wp;

CLOSE exp_cur;

select * from temp_explode_string;
select @name_cnt;

END$$

我得到的是什么

+----------+--------------+
| emp_name | wp           |
+----------+--------------+
| john     | CA           |
| john     | philadelphia |
| paul     | CA           |
+----------+--------------+

我正在寻找的结果是

temp_explode_string

+----------+--------------+
| emp_name | wp           |
+----------+--------------+
| john     | CA           |
| john     | philadelphia |
| carl     | New Jersey   |
| carl     | oklahoma     |
| paul     | New Jersey   |
| paul     | oklahoma     |
| paul     | CA           |
+----------+--------------+

任何人都可以帮帮我! 对不起,如果代码中的任何基本错误都是游标新手。

2 个答案:

答案 0 :(得分:1)

通过加入一些获得一系列数字的子查询来实现此目的: -

SELECT DISTINCT ename AS emp_name, SUBSTRING_INDEX(SUBSTRING_INDEX(table1.work_place, ',', 1 + units.i + tens.i * 10), ',', -1) AS wp  
FROM table1
CROSS JOIN (SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) units
CROSS JOIN (SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) tens

请注意,这将在任何一个work_place行中处理最多100个逗号分隔值。

这样可以节省使用游标或基本查询以外的任何内容。

答案 1 :(得分:0)

试试这个:

BEGIN

       DECLARE pos1 INTEGER;
       DECLARE var1 VARCHAR(255);
       DECLARE var2 VARCHAR(255);
       DECLARE varFinal VARCHAR(255);
       DECLARE list1 VARCHAR(255);
       DECLARE done INT DEFAULT 0;

    DECLARE cur1 CURSOR FOR Select ename,work_place from table1;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

    DROP Temporary Table if exists temp_explode_string;
    CREATE Temporary Table  temp_explode_string (emp_name varchar(11), wp varchar(55)) ;

    OPEN cur1;
    REPEAT
        FETCH cur1 INTO var1, var2;
        IF NOT done THEN

            SET list1 = var2;
            SELECT LOCATE(',',list1) INTO pos1;

            WHILE (LENGTH(list1) >0) DO
                IF (pos1=0) THEN
                    SELECT list1 INTO varFinal;
                    SET list1 = '';
                ELSE
                    SELECT SUBSTRING(list1,1,pos1-1) INTO varFinal;
                    SELECT SUBSTRING(list1 FROM pos1+1) INTO list1;
                    SELECT LOCATE(',',list1) INTO pos1;
                END IF;

                Insert into temp_explode_string values (var1,varFinal);

            END WHILE;

        END IF;

    UNTIL done
    END REPEAT;
    CLOSE cur1;

    select * from temp_explode_string;


END