2017-05-23 12 views
1

私は4つの列を持ちます.2つの列はコンマで区切られています。私はそれらのコンマ区切り値の個々のレコードを取得しようとしています。列に基づいてレコードを区切る

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番目の要素を取得するために数字の2つの派生テーブルを使用して一つの方法である:あなたが列に持つことができる最大のコンマは何

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), '%') 
関連する問題