我有三個表分隔值如逗號在SQL Server三個表
用戶
+--+------+
|Id|Name |
+--+------+
|1 |Ram |
+--+------+
|2 |Rama |
+--+------+
|3 |Leesa |
+--+------+
|4 |Kelvin|
+--+------+
角色
+--+-------+
|Id|Name |
+--+-------+
|1 |Admin |
+--+-------+
|2 |FA |
+--+-------+
|3 |Testing|
+--+-------+
|4 |IT |
+--+-------+
用戶角色
+--+-------+-------+
|Id|User Id|Role Id|
+--+-------+-------+
|1 |1 |1 |
+--+-------+-------+
|2 |1 |2 |
+--+-------+-------+
|3 |2 |3 |
+--+-------+-------+
|4 |2 |1 |
+--+-------+-------+
|5 |3 |2 |
+--+-------+-------+
|6 |3 |3 |
+--+-------+-------+
|7 |4 |4 |
+--+-------+-------+
|8 |4 |2 |
+--+-------+-------+
從這些3我想要這樣的輸出表
+--+---------+--------+
|Id|User Name|Roles |
+--+---------+--------+
|1 |Ram |Admin,FA|
+--+---------+--------+
你試過了什麼? – nrsharma
我試圖就這樣 –
與CTE爲 ( 選擇a.id,c.name爲用戶1, b.name如基於role1 從一個的UserRole加入基於role1 B關於a.roleid = b.id加入第C USER1Ç .id = a.userid ) select user1,stuff((從cte UL1選擇','+ role1,其中UL1.user1 = UL.user1 ORDER BY user1 FOR XML PATH('')),1,2,' ')as [role1] from cte UL GROUP BY user1對不起答案Post box Not at show on my side所以... –