日期:2014-05-16  浏览次数:20984 次

sql合并数据表,有多条变成一条


若不清楚,可http://zhidao.baidu.com/question/427405338.html?quesup2&oldq=1

------解决方案--------------------
SQL code

create table t1(id int ,name varchar(20),c1 varchar(20),c2 varchar(20),c3 varchar(20) );

insert into t1 values (1,'wc','as','','');
insert into t1 values (2,'wc','asd','','');
insert into t1 values (3,'wc','','fdf','');
insert into t1 values (4,'wc','','','er');
insert into t1 values (5,'wd','fg','','');
insert into t1 values (6,'wd','fgo','','');
insert into t1 values (7 ,'wd','','ko',''); 
commit;

select name,
       substr(c1,instr(c1,',',-1)+1,length(c1)) c1,
       substr(c2,instr(c2,',',-1)+1,length(c2)) c2,
       substr(c3,instr(c3,',',-1)+1,length(c3)) c3
from (select name,
         wm_concat(c1) c1,
         wm_concat(c2) c2,
         wm_concat(c3) c3
     from t1
     group by name)


     name    c1    c2    c3
----------------------------------------
1    wc    asd    fdf    er
2    wd    fgo    ko