有人可以回答这种情况吗? 假设我有2个表:
表图书,其值为Batch_no和Title
Batch_no - 标题
1 - A
2 - B
和;
表 Book_Authors ,其值为Batch_no和Author_no
Batch_no - 作者_no
1 - 1
1 - 2
1 - 3
2 - 1
我应该如何将值合并为1行,看起来像这样
Batch_no作者
1 - 1,2,3
2 - 1
非常感谢任何帮助......非常感谢!
答案 0 :(得分:0)
如果你看看这里:http://www.simple-talk.com/sql/t-sql-programming/concatenating-row-values-in-transact-sql/
有几种技巧可以做到这一点。
根据您的情况调整,这里看起来很简单:
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;