我有一张这样的桌子
id | Name
===========
1 | A
2 | A
3 | A
4 | B
5 | B
6 | C
我正在从tbl那里选择select id,其中name =“ A”,我想像这样在单个变量中获得所有三个id(1,2,3),并以逗号分隔,然后在另一个变量中使用该变量选择具有IN子句的查询,有什么帮助吗?
答案 0 :(得分:1)
正如其他人指出的那样,使用listagg()
应该可以解决问题:
SELECT listagg(id, ',') WITHIN GROUP (ORDER BY id) as concatenation
FROM mytable
WHERE name = 'A'
答案 1 :(得分:0)
这有效:
create table ns_1111(col1 number,col2 varchar(20));
insert into ns_1111 values(1,'A');
insert into ns_1111 values(2,'A');
insert into ns_1111 values(3,'A');
insert into ns_1111 values(4,'B');
insert into ns_1111 values(5,'B');
insert into ns_1111 values(6,'C');
SELECT * FROM ns_1111;
select * from (SELECT LISTAGG(col1, ', ') WITHIN GROUP (ORDER BY col1)
FROM ns_1111 group by col2) where rownum<=1 ;
输出:
1, 2, 3