场景
将账户表中同一个用户的账户号合并显示在一条记录中select name, listagg(account_name, ',') within group(order by name) from user_account group by name;
建表sql语句create table user_account(
id number(12,0) primary key,
name varchar(50),
account_name varchar(50),
balance number(10,2)
);
insert into user_account values(1, 'wangwu', 'wangwuAccount1', 600);
insert into user_account values(2, 'wangwu', 'wangwuAccount2', 400);
insert into user_account values(3, 'lisi', 'lisiAccount1', 200);
insert into user_account values(4, 'lisi', 'lisiAccount2', 1000);
insert into user_account values(5, 'zhangsan', 'zhangsanAccount1', 500);
insert into user_account values(6, 'zhangsan', 'zhangsanAccount2', 500);
insert into user_account values(7, 'zhangsan', 'zhangsanAccount3', 500);
查询sql语句
一般查询:select * from user_account order by id
结果:
将account_name字段根据name字段进行合并select name, listagg(account_name, ',') within group(order by name) from user_account group by name;
结果:
---------------------
参考链接:https://blog.csdn.net/qq_36743920/article/details/80291166
联系客服