我在sql server中有兩個表User
和UserRoles
。 User
表具有基本的用戶信息,例如, UserId,Name
等和UserRoles
有像UserId,RoleName
列。這兩個表之間有一對多的關係,即一個用戶可以有多個角色。如何在sql server中連接一對多記錄
用戶
UserId Name
1 A
2 B
3 C
的UserRole
UserId Rolename
1 Manager
1 Event Organiser
2 Supervisor
2 Employee
2 Some otherRole
我需要寫在SQL將返回象下面的查詢。即串聯一對多記錄到一個單一的字符串
UserId Roles
1 Manager,Event Organiser
2 Supervisor,Employee,Some otherRole
LUV的,偉大的答案 - 錯字的級聯。對不起,我不能編輯我沒有這個小改動的代表。 – Liath
@Luv感謝哥們 – rumi