Sql连接maptables并用逗号分隔结果

时间:2016-04-14 18:43:04

标签: sql-server tsql

我有三张桌子。我已使用查询连接它,但我想在一行中用逗号分隔结果,而不是在不同的行上显示结果。

第一张表

 ProgrammeId | Name
====================
1 |Software Engineering                                         
2 | Game Design

第二张表

 CoordinatorId| CoordinatorName
====================
1 |Bob                                         
2 | Ted

3d表格

 ProgrammeId| CoordinatorId
====================
1 | 1                                         
1 | 2
2 | 1

这是一个查询。

 SELECT a.ProgrammeId, a.Name, c.NameCoordinator
FROM Programme a
--Joining maptable for Programme and Coordinator
INNER JOIN ProgrammeCoordinators b
ON a.ProgrammeId = b.ProgrammeId
INNER JOIN Coordinator c
ON b.CoordinatorId = c.CoordinatorId

输出将是这样的:

 ProgrammeId| Name | CoordinatorName
==================================
1 | Software Engineer | Bob                                        
1 | Software Engineer  | Ted
2 | Game Design | Ted

如何分开才能显示:

1 | Software Engineer | Bob, Ted

1 个答案:

答案 0 :(得分:0)

如果您的姓名长度为100:

SELECT a.ProgrammeId, a.Name(Name,' ',100), c.NameCoordinator
FROM Programme a
--Joining maptable for Programme and Coordinator
INNER JOIN ProgrammeCoordinators b
ON a.ProgrammeId = b.ProgrammeId
INNER JOIN Coordinator c
ON b.CoordinatorId = c.CoordinatorId