How to return 1 single row data from 2 different tables with dynamic contents in sql

后端 未结 1 545
情书的邮戳
情书的邮戳 2021-01-28 18:45

Can someone provide answer to this situation?? Suppose I have 2 tables:

Table Books with values Batch_no and Title

Batch_no - Title
1 - A<

相关标签:
1条回答
  • 2021-01-28 19:07

    If you take a look here: http://www.simple-talk.com/sql/t-sql-programming/concatenating-row-values-in-transact-sql/

    there are several techniques you can do this.

    Adapting for your situation, here is one that looks simple:

        select batch_no, LEFT(booksauthors, len(booksauthors)-1) as Authors from 
    (SELECT ba.Batch_no,
    
          ( SELECT cast(ba1.Author_no as varchar(10)) + ','
    
               FROM Book_Authors ba1
    
              WHERE ba1.Batch_no = ba.Batch_no
    
              ORDER BY Author_no
    
                FOR XML PATH('') ) AS BooksAuthors
    
          FROM Book_Authors ba
    
          GROUP BY Batch_no )A;
    
    0 讨论(0)
提交回复
热议问题