在SQL中以特定字符分隔值

问题描述

我有一张只有一列的表:

Val A
Val B
Val C,Val B,Val D
Val A,Val F,Val A

我的问题是在这种情况下,如何在特定字符后分割值,这样我的每一行只能有一个这样的字符:

Val A
Val B
Val C
Val B
Val D
Val A
Val F
Val A

我不重要,但是我正在使用MysqL Workbench。 预先感谢。

解决方法

您可以使用File* stdin。一种方法是:

substring_index()

您需要添加一个单独的子查询,最多不超过任何行中的元素数量。

编辑:

我真的不喜欢重复的答案。我建议使用递归CTE:

select substring_index(col,';',1)
from t
union all
select substring_index(substring_index(col,2),-1)
from t
where col like '%;%'
union all
select substring_index(substring_index(col,3),-1)
from t
where col like '%;%;%';

Here是db 小提琴。