Home  >  Article  >  Database  >  MySQL逗号分割字段的行列转换技巧_MySQL

MySQL逗号分割字段的行列转换技巧_MySQL

WBOY
WBOYOriginal
2016-06-01 13:48:29981browse

bitsCN.com

前言:

由于很多业务表因为历史原因或者性能原因,都使用了违反第一范式的设计模式。即同一个列中存储了多个属性值(具体结构见下表)。

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

表数据:

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

正文:

#需要处理的表create table tbl_name (ID int ,mSize varchar(100));insert into tbl_name values (1,'tiny,small,big');insert into tbl_name values (2,'small,medium');insert into tbl_name values (3,'tiny,big');#用于循环的自增表create table incre_table (AutoIncreID int);insert into incre_table values (1);insert into incre_table values (2);insert into incre_table values (3);
bitsCN.com
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn