【问题标题】:Append the results of subquery in oracle在oracle中追加子查询的结果
【发布时间】:2018-08-31 18:20:04
【问题描述】:

假设我在 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 以逗号分隔。

【问题讨论】:

    标签: database oracle subquery concatenation oracle-sqldeveloper


    【解决方案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;
    

    【讨论】:

    • @SagarShiroya 那又怎样?
    猜你喜欢
    • 1970-01-01
    • 2016-01-29
    • 1970-01-01
    • 1970-01-01
    • 2011-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多