oracle中如何将查询结果连接到同一列?

Meh*_*nce 0 sql oracle

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

查询结果如下;

ID     EMAIL
--     -----
1      abc@abc.com
2      xyz@xyz.com
3      def@def.com
Run Code Online (Sandbox Code Playgroud)

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

我怎样才能在 Oracle 中实现这一目标?

Tec*_*hDo 5

请尝试以下查询,该查询适用于 ORACLE 11G:

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

SQL 小提琴演示

或者

SELECT 
  (RTRIM(XMLAGG(xmlelement(X, EMAIL||',')order by id).extract('//text()'),',')) list
FROM operators
WHERE op_code=1
Run Code Online (Sandbox Code Playgroud)