专业编程教程与实战项目分享平台

网站首页 > 技术文章 正文

oracle的listagg函数,可以把多行转为一个字符串

ins518 2025-08-01 17:23:09 技术文章 7 ℃ 0 评论

oracle的listagg函数可以把多行转为一个字符串,用起来很方便,示例如下:

with t1 as  (select '001' as itemcode,'苹果' as itemname,'01' as itemtype from dual
union all select '002','梨','01' from dual union all select '003','香蕉','01' from dual union all select '004','桔子','01' from dual
union all select '005','土豆','02' from dual union all select '006','白菜','02' from dual union all select '007','黄瓜','02' from dual) 
select listagg(t1.itemcode||t1.itemname,',') WITHIN GROUP(ORDER BY t1.itemtype) as itemlist from t1
;
with t1 as  (select '001' as itemcode,'苹果' as itemname,'01' as itemtype from dual
union all select '002','梨','01' from dual union all select '003','香蕉','01' from dual union all select '004','桔子','01' from dual
union all select '005','土豆','02' from dual union all select '006','白菜','02' from dual union all select '007','黄瓜','02' from dual) 
select t1.itemtype, listagg(t1.itemcode||t1.itemname,',') WITHIN GROUP(ORDER BY t1.itemtype) as itemlist from t1
group by t1.itemtype
;

20250720

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表