I have three table:
book_shelf
->pkid
and other columnsbook_author
->pkid
,fkid_book_shelf
,fkid_author
author
->pkid
,name
, etc.
For a row in book_shelf
in want to get author names as comma separated. If a bookshelf entity has two author then two row is inserted in book_author
table.
Here is what I tried
(select BookShelf.pkid as SrNo,stuff((select ',' + a.name
from
lib.book_shelf bs inner join
lib.book_author ba on bs.pkid=ba.fkid_book_shelf
inner join lib.author a on a.pkid=ba.fkid_authors
for xml path ('')
), 1, 1, '') as Author from lib.book_shelf BookShelf)
But it selects all the author name for every book_shelf
row.