0

我有这样的选择查询;select id, email from operators where op_code = 1

查询结果如下;

ID     EMAIL
--     -----
1      abc@abc.com
2      xyz@xyz.com
3      def@def.com

但我想要这种格式的电子邮件,abc@abc.com,xyz@xyz.com,def@def.com

如何在 Oracle 中实现这一点?

4

1 回答 1

5

Please try below query, which works in 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
于 2013-10-31T09:13:32.680 回答