在 ORACLE 中使用定界符连接 SELECT 中的值

Concatenate values from SELECT with delimiter in ORACLE

我有 table products 并且我想创建字符串:

http://localhost/app/feeds/send?type=myType&ids=1,2,3,...,100
http://localhost/app/feeds/send?type=myType&ids=101,102,103,...,200

我创建了 sql 查询:

select concat('http://localhost/app/feeds/send?type=myType' || CHR(38) || 'ids=' , product_id) from products where isin like 'AC%' and status in
('Active', 'Created', 'Live')
and
((date>to_date('07.05.2021','dd.MM.yyyy') or date is null));

这似乎几乎是我需要的,但结果是:

http://localhost/app/feeds/send?type=myType&ids=1
http://localhost/app/feeds/send?type=myType&ids=2
http://localhost/app/feeds/send?type=myType&ids=3

如何将此查询更改为带有分隔符 , 的 return 100 ids

您可以在 Oracle 中使用 LISTAGG 函数。

select 'http://localhost/app/feeds/send?type=myType' || CHR(38) || 'ids=' ||

  LISTAGG(product_id,',') WITHIN GROUP (ORDER BY product_id)

from products where isin like 'AC%' and status in
('Active', 'Created', 'Live')
and
((pdate>to_date('07.05.2021','dd.MM.yyyy') or pdate is null));

DB Fiddle

删除 Concat 函数,您不需要它。

使用任何将 id 1 到 100 与 101 到 200 分开的字段添加 Group By 子句。

然后使用 ListAgg() 聚合函数(如 ListAgg(id,','))让您的查询 return 列。第一个参数是您要聚合的字段 (id),第二个参数是两条记录之间使用的分隔符。