本人现在要做一数据导出合并,例如:id  xm   xb   nla   aaa  女   10
a   bbb  女   20
b   ccc  男   30
b   ddd  女   40
c   eee  女   50以 id 为主的查询导出到另外一张新表,只要id相同,相同结果就合并并且放在一个字段中以指定分割符号分割,结果是:id    newsa     aaa_女_10|bbb_女_20
b     ccc_男_30|ddd_女_40
c     eee_女_50哪位高手知道这样的数据导出合并如何操作,用的是mysql数据库

解决方案 »

  1.   

    CREATE TABLE newtable(
    id varchar( 20 ) ,
    news text
    );
    insert into newtable SELECT id,GROUP_CONCAT(CONCAT_WS('_',xm,xb,nl) SEPARATOR '|') as news FROM oldtable GROUP BY id;
      

  2.   

    现在增加难度,如果 newtable 这张表本来存在,它和oldtable 表就 id 相同,现在在newtable中加字段 news,用update 语句怎么得出那结果了 
      

  3.   

    你可以利用replact into语句,这样newtable之前没有导入的id,也可以导进去,用update只能更新newtable和oldtable共有的id.alter table newtable add unique a (`id`) ;
    replace into newtable SELECT id,GROUP_CONCAT(CONCAT_WS('_',xm,xb,nl) SEPARATOR '|') as news FROM oldtable GROUP BY id;
    alter table newtable drop index a;
      

  4.   

    oldtable里有的id , newtable里一定有,所以用update完全足够~~
      

  5.   


    update newtable,(SELECT id,GROUP_CONCAT(CONCAT_WS('_',xm,xb,nl) SEPARATOR '|') as news FROM oldtable GROUP BY id) a set newtable.news = a.news where newtable.id=a.id