在oracle中附加子查询的结果

时间:2018-03-22 10:39:05

标签: database oracle subquery concatenation oracle-sqldeveloper

假设我在oracle中查询如下:

SELECT author_id, author_name,
  (
     SELECT book_title FROM books
     WHERE author_id = a.author_id
   )
FROM author a

在子查询中,特定的author_id可以有多本书。但Oracle不允许从子查询返回多行。那么如何在oracle中为同一个作者分别添加逗号分隔的不同book_title。

1 个答案:

答案 0 :(得分:1)

LISTAGG可能有所帮助:

select 
  a.author_id, 
  a.author_name, 
  listagg(b.book_title, ',') within group (order by null) list_of_books
from author a join books b on b.author_id = a.author_id
group by a.author_id, a.author_name;