您好,欢迎访问一九零五行业门户网

MySQL逗号分割字段的行列转换测试改进

p由于很多业务表因为历史原因或者性能原因,都使用了违反第一范式的设计模式。即同一个列中存储了多个属性值(具体结构见下表)。/pp这种模式下,应用常常需要将这个列依据分隔符进行分割,并得到列转行的结果。/p span class=cnblogs_code_copy/spanp style
由于很多业务表因为历史原因或者性能原因,都使用了违反第一范式的设计模式。即同一个列中存储了多个属性值(具体结构见下表)。
这种模式下,应用常常需要将这个列依据分隔符进行分割,并得到列转行的结果。

表数据:

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);
select a.id,substring_index(substring_index(a.msize,',',b.autoincreid),',',-1) from tbl_name ajoinincre_table bon b.autoincreid (length(a.msize) - length(replace(a.msize,',',''))+1)order by a.id;


原理分析:这个join最基本原理是笛卡尔积。通过这个方式来实现循环。
以下是具体问题分析:
length(a.size) - length(replace(a.msize,',',''))+1  表示了,按照逗号分割后,改列拥有的数值数量,下面简称n
select a.id,substring_index(substring_index(a.msize,',',b.autoincreid),',',-1) from tbl_name ajoinincre_table bon b.autoincreid (length(a.msize) - length(replace(a.msize,',',''))+1)order by a.id;
原理分析:这个join最基本原理是笛卡尔积。通过这个方式来实现循环。
以下是具体问题分析:
length(a.size) - length(replace(a.msize,',',''))+1  表示了,按照逗号分割后,改列拥有的数值数量,下面简称n
join过程的伪代码:
根据id进行循环
{
判断:i 是否
{
获取最靠近第 i 个逗号之前的数据, 即 substring_index(substring_index(a.msize,',',b.id),',',-1)
i = i +1 
}
id = id +1 
}
总结:这种方法的缺点在于,我们需要一个拥有连续数列的独立表(这里是incre_table)。并且连续数列的最大值一定要大于符合分割的值的个数。
例如有一行的msize 有100个逗号分割的值,那么我们的incre_table 就需要有至少100个连续行。
当然,mysql内部也有现成的连续数列表可用。如mysql.help_topic: help_topic_id 共有504个数值,一般能满足于大部分需求了。
改写后如下:
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;
测试实例:



-- select help_topic_id from mysql.help_topic-- eg.把一个字段用“,”分隔开组合select group_concat(user_id order by user_id asc) as nids from admin_userselect b.did,group_concat(b.sid order by adjustment desc,similar desc) from test b group by b.did -- 1.如果多个导购同1张单的先分解-- 加时间段select a.djbh,a.je,substring_index(substring_index(a.dgy_list_id,',',b.help_topic_id+1),',',-1) from ipos_qtlsd ajoinmysql.help_topic bon b.help_topic_id < (length(a.dgy_list_id) - length(replace(a.dgy_list_id,',',''))+1) and a.djbh='bp0102_qtsy000070'order by a.djbh;-- 2.取平均值-- select help_topic_id from mysql.help_topic-- 1.如果多个导购同1张单的先分解-- @zddm-- @ rqselect a.djbh,substring_index(substring_index(a.dgy_list_id,',',b.help_topic_id+1),',',-1) as fjid,substring_index(substring_index(a.dgy_list_mc,',',b.help_topic_id+1),',',-1) as fjmc,format(a.je/(length(a.dgy_list_id) - length(replace(a.dgy_list_id,',',''))+1),2) as fjje,je from ipos_qtlsd ajoinmysql.help_topic bon b.help_topic_id < (length(a.dgy_list_id) - length(replace(a.dgy_list_id,',',''))+1) and a.rq between unix_timestamp('2016-04-01') and unix_timestamp('2016-05-01')and a.djbh='gd_151125000001'order by a.djbh;-- gd_151125000001 --3.分解后的指标-- select help_topic_id from mysql.help_topic-- 1.如果多个导购同1张单的先分解-- @khdm_change 终端代码-- @start_time 开始时间-- @end_time结束时间-- select * from ipos_qtlsd where djbh='gd_151125000001'set @khdm_change ='bp0102';set @start_time=unix_timestamp('2016-04-01');set @end_time=unix_timestamp('2016-05-01');select fjid,fjmc,sum(fjje) from(select a.zddm,a.zdmc,a.djbh, substring_index(substring_index(a.dgy_list_id,',',b.help_topic_id+1),',',-1) as fjid, substring_index(substring_index(a.dgy_list_mc,',',b.help_topic_id+1),',',-1) as fjmc, format(a.je/(length(a.dgy_list_id) - length(replace(a.dgy_list_id,',',''))+1),2) as fjje, je from ipos_qtlsd a join mysql.help_topic b on b.help_topic_id < (length(a.dgy_list_id) - length(replace(a.dgy_list_id,',',''))+1) and a.rq between @start_time and @end_timeand a.zd_id=(select id from com_base_kehu where khdm=@khdm_change)) aagroup by fjid,fjmc-- and a.djbh='gd_151125000001'-- order by a.djbh;

其它类似信息

推荐信息