2016-09-05 43 views
10

分離字符串列表,並在MySQL 用新值替換同一名單我有我的TABLE_1表 TABLE_1(目前保存的結構)如何分離一個字符串,並重新構建它

code  value 
12_A  ["A","B","C","D"] 
12_B  ["E","F","G","H"] 
12_3  ["I","J","K","L"] 

但每個代碼如下數據具有不同描述的不同值。像::

code value  description 
12_A A   Apple 
12_A B   Ball 
12_A C   Cat 
12_A D   Dog 
12_B E   Eagle 
12_B F   Flag 
.  .   . 
.  .   . 
.  .   . 

我不得不從TABLE_1分隔值列表,並 需要在同一個表,即TABLE_1再次保存(在此結構)::

code   value 
12_A   ["Apple","Ball","Cat","Dog"] 
12_B   ["Eagle","Flag",.......] 
12_3   [......................] 
+0

字符串值是壞的分貝設計 – Jens

回答

4

您可以使用GROUP_CONCAT()

UPDATE Table1 s 
SET s.value = (SELECT t.code,CONCAT('["', 
          GROUP_CONCAT(t.description ORDER BY t.description SEPARATOR '","'), 
          ']') 
      FROM Table_With_val t 
      WHERE t.code = s.code 
       AND s.value LIKE CONCAT('%"',t.value,'"%')) 

你沒有提供任何確鑿的信息,我假設你提供的第二個數據樣本是一個現有的表,table1是你想要的表更新。

NOTE:這是一個不好的數據庫結構!它將在未來尤其是在需要加入時最大限度地引起問題。我強烈建議您規範化數據並將每個描述和值存儲在自己的記錄中。

+0

雖然經歷這樣的邏輯,我遇到一個問題, 如果我只有 碼值 12_A [「A」,「B」在TABLE_1, 我們在table_2, 中代碼12_A的所有值都有A,B,C,D,E的描述,然後在table_1中更新所有[「Apple」,「Ball」,「Cat」,「Dog」只有[「蘋果」,「球」] –

+0

因此,table_1和table_2不匹配?你應該在問題中提到的東西.. – sagi

+0

是的,table_2有固定值,但在table_1中,可以有所有值或更少的值在表2中有描述 –

4

您可以創建一個函數,在該函數中您可以將您的字符串列表作爲參數傳遞給您的示例[「A」,「B」,「C」,「D」]將作爲參數。該函數將打破字符串,並將連接描述。你可以使用的功能的例子如下:

DELIMITER $$ 

DROP FUNCTION IF EXISTS codeToDesc$$ 

CREATE FUNCTION codeToDesc(commaSeperatedCodeList TEXT) RETURNS TEXT CHARSET utf8 
BEGIN 
DECLARE finalString TEXT; 
DECLARE inputCodeList TEXT; 
DECLARE codeName VARCHAR(255); 
DECLARE codecount BIGINT(5); 

SET finalString=''; 
SET inputCodeList = REPLACE(REPLACE(REPLACE(commaSeperatedCodeList,'[',''),']',''),'"',''); 

DROP TEMPORARY TABLE IF EXISTS test.code_table; 
DROP TEMPORARY TABLE IF EXISTS test.code_count; 
CREATE TEMPORARY TABLE test.code_table (CODE VARCHAR(255)); 
CREATE TEMPORARY TABLE test.code_count (countNo BIGINT(11)); 

INSERT INTO test.code_count(countNo) SELECT(LENGTH(inputCodeList)-LENGTH(REPLACE(inputCodeList,',','')) + 1); 

BEGIN 

    DECLARE table_cursor CURSOR FOR SELECT countNo FROM test.code_count; 
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET codecount = (SELECT countNo FROM test.code_count ORDER BY countNo ASC LIMIT 1); 

    OPEN table_cursor; 
    readLoop1: LOOP 

    FETCH table_cursor INTO codecount; 

     IF codecount=0 THEN 
       LEAVE readLoop1; 
     END IF; 

    SET codeName=(SELECT SUBSTRING_INDEX(inputCodeList,',',1)); 
    INSERT INTO test.code_table(CODE) SELECT codeName; 
    SET inputCodeList=(SELECT TRIM(BOTH ',' FROM REPLACE(inputCodeList,codeName,''))); 
    INSERT INTO test.code_count(countNo) SELECT codecount-1;  
    SET codeName=''; 

    END LOOP; 
    CLOSE table_cursor; 
END;  

    -- use your code and description here, i guess those should be fixed 
    SELECT CONCAT('["',REPLACE(GROUP_CONCAT(CASE WHEN CODE='A' THEN 'Apple' 
       WHEN CODE = 'B' THEN 'Ball' 
       WHEN CODE = 'C' THEN 'Cat' 
       WHEN CODE = 'D' THEN 'Dog' 
       ELSE '' END),',','","'),'"]') INTO finalString FROM test.code_table; 

    RETURN finalString; 

END$$ 

DELIMITER ; 

試試這個,讓我知道你是否有任何問題發生。在這種格式

相關問題