我有四列。两列以逗号分隔。我正在尝试获取这些逗号分隔值的单独记录。
col col2 col3 col4
------------------------
1 1,2 2,3 4
2 3,4 5 7
4 5 3 5
我的结果集swill
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
我尝试过很多次。但无法获得精确的数据集。提前致谢
答案 0 :(得分:0)
这是一个方法,它使用两个派生的数字表来从列表中获取 nth 元素:
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), '%')