我有三个表,例如
用户
+--+------+
|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|
+--+---------+--------+
答案 0 :(得分:0)
您可以通过创建存储过程来完成。
CREATE PROC UserRoles
(
@UserId int
)
AS
begin
DECLARE @roles NVARCHAR(MAX) = ''
SELECT @roles = @roles + ',' + t.Roles
FROM (SELECT r.NAME AS Roles FROM [User Role] ur
Inner JOIN [ROLE] r ON r.Id = ur.RoleId
INNER JOIN User1 u ON u.Id = ur.UserID WHERE u.Id = @UserId) t
SET @roles = RIGHT(@roles, LEN(@roles)-1)
SELECT u.Id AS ID, u.NAME AS UserName, @roles AS Roles FROM USER1 u WHERE u.Id = @UserId
end
答案 1 :(得分:0)
我想为未来的SQL标准提出一些函数CONCATROWS。
SELECT page_id,CONCATROWS('\n',partext) AS pagetext FROM paragraph ORDER BY parposition GROUP BY page_id
我需要这个功能超过我可以计数的时间......不是我需要它很糟糕,但它会让生活变得如此简单。