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

mysql及oracle中的group_contact小结
今天有个朋友问,如下的合拼,如何写法,分别在MYSQL和ORACLE:

数据库的结构如下:

no    item
01    AA
01    BB
02    CC
02    DD
02    EE
03    FF
04    GG
04    HH

希望将no相同的列整合为一条记录如下
no    items
01    AA,BB
02    CC,DD,EE
03    FF
04    GG,HH


MYSQL中,直接有group_contact函数了,如下:
  select id,group_contact(items)  from TABLE group by id

而oracle中,对应的有wm_contact,摘抄例子如下:
select id,wmsys.wm_concat(items) name from table
group by id;
 

再看SQL SERVER 2005:
--SQL2005中的方法
 create table tb(id int, value varchar(10))   insert into tb values(1, 'aa')   insert into tb values(1, 'bb')   insert into tb values(2, 'aaa')   insert into tb values(2, 'bbb')   insert into tb values(2, 'ccc')   go     select id, [values]=stuff((select ','+[value] from tb t where id=tb.id for xml path('')), 1, 1, '')   from tb   group by id     /*   id          values   ----------- --------------------   1          aa,bb   2          aaa,bbb,ccc