2013-10-31 70 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' –