2017-05-23 87 views
1

我有四列。兩列用逗號分隔。我正在嘗試爲這些逗號分隔值獲取單個記錄。根據列分隔記錄

col col2 col3 col4  
------------------------ 
1  1,2 2,3 4 
2  3,4 5  7 
4  5  3  5 

我的結果集泔水是

col1 col2 col3 col4 
-------------------------- 
1  1  2  4 
1  1  3  4 
1  2  2  4 
1  2  3  4 
2  3  5  7 
2  4  5  7 
4  5  3  5 

我已經嘗試了許多。但無法獲得確切的數據集。在此先感謝

+2

什麼是你可以擁有的最大逗號列? –

+0

不是那麼容易,因爲我明白:https://stackoverflow.com/questions/2696884/split-value-from-one-field-to-two –

+0

最多5個逗號 – mano

回答

0

下面是用兩個數字派生表來從列表中的第n個元素一個方法:

select col1, 
     substring_index(substring_index(col2, ',', n2.n), ',', -1) as col2, 
     substring_index(substring_index(col3, ',', n3.n), ',', -1) as col3, 
     col4 
from t join 
    (select 1 as n union all select 2 union all select 3 union all select 4 union all select 5 
    ) n2 
    on t.col2 like concat(repeat('%,', n2.n - 1), '%') join 
    (select 1 as n union all select 2 union all select 3 union all select 4 union all select 5 
    ) n3 
    on t.col3 like concat(repeat('%,', n3.n - 1), '%')