2013-10-31 47 views
0

我有这样的选择查询; select id, email from operators where op_code = 1如何连接oracle中同一列的查询结果?

查询结果如下;

ID  EMAIL 
--  ----- 
1  [email protected] 
2  [email protected] 
3  [email protected] 

但我需要的电子邮件这样的格式,[email protected],[email protected],[email protected]

我如何在Oracle中实现这一目标?

+0

http://stackoverflow.com/questions/1614373/how-to-使用-coalesce-in-oracle-to-combine-data-from-two-rows –

回答

4

请尝试以下查询,在ORACLE 11G工作:

select 
    listagg(email, ',') 
    within group (order by id) as list 
from operators 
where op_code=1 

SQL Fiddle Demo

OR

SELECT 
    (RTRIM(XMLAGG(xmlelement(X, EMAIL||',')order by id).extract('//text()'),',')) list 
FROM operators 
WHERE op_code=1 
+0

发出此错误:'ORA-00923:FROM关键字找不到预期的地方' –

+0

哪个是您的数据库版本? – TechDo

+0

'Oracle Database 10g企业版版本10.2.0.5.0 - 64bi' –