【MYSQL】mysql把字段进行逗号分隔成多条数据

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

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

表数据:

ID  Value

1 tiny,small,big

2 small,medium

3 tiny,big

期望得到结果:

ID Value

1 tiny

1 small

扫描二维码关注公众号,回复: 5418041 查看本文章

1 big

2 small

2 medium

3 tiny

3 big

 备注:a.mSize 改为对应字段名

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

https://www.2cto.com/database/201704/635342.html

另:https://blog.csdn.net/c_molione/article/details/82968554

猜你喜欢

转载自blog.csdn.net/bandaoyu/article/details/87918242