如何使用SQL Server返回逗号分隔的字符串?
[MyAuthorize("Home")]
public class HomeController : Controller
{ }
并让它返回如下结果:
select ID,
(<<SomeQuery from TableB B where (B.Id = A.TableBId)>>)
from TableA A
答案 0 :(得分:3)
您可以使用STUFF(),See Demo Here
SELECT ID
,STUFF((SELECT ', ' + CAST(data AS VARCHAR(10)) [text()]
FROM B
WHERE TableBId = A.ID
FOR XML PATH(''), TYPE)
.value('.','NVARCHAR(MAX)'),1,2,' ') Comma_Output
FROM A
GROUP BY ID