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

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

 备注: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

posted on   bdy  阅读(307)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示