网站建设知识
mysql把字段进行逗号分隔成多条数据
2025-07-22 11:15  点击:0

mysql把字段进行逗号分隔成多条数据,由于很多业务表因为历史原因或者性能原因,都使用了违反第一范式的设计模式。即同一个列中存储了多个属性值(具体结构见下表)。

这种模式下,应用常常需要将这个列依据分隔符进行分割,并得到列转行的结果。

表数据:

ID  Value

1 tiny,small,big

2 small,medium

3 tiny,big

期望得到结果:

ID Value

1 tiny

1 small

1 big

2 small

2 medium

3 tiny

3 big

select a.ID,substring_index(substring_index(a.mSize,',',b.help_topic_id+1),',',-1) from tbl_name ajoinmysql.help_topic bon b.help_topic_id < (length(a.mSize) - length(replace(a.mSize,',',''))+1)order by a.ID;