admin管理员组

文章数量:1529458

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

1

createtabletb(idint,valuevarchar(10))insertintotbvalues(1,'aa')insertintotbvalues(1,'bb')insertintotbvalues(2,'aaa')insertintotbvalues(2,'bbb')insertintotbvalues(2,'ccc')goselectid,[values]=stuff((select','+[value]fromtbtwhereid=tb.idforxmlpath('')),1,1,'')fromtbgroupbyid/*idvalues-------------------------------1aa,bb2aaa,bbb,ccc

欢迎大家阅读《mysql及oracle中的group_contact总结》,跪求各位点评,by 搞代码

微信 赏一包辣条吧~

支付宝 赏一听可乐吧~

本文标签: ContactmysqlgroupmysqlgroupcontactORACLE