获取用HTML包裹的一行中的用户角色

时间:2017-04-13 07:24:14

标签: sql-server tsql sql-server-2012

我想将每个用户的角色写在一行中,包含在一些HTML中。 有三个表:

BAUSER - users
BAUSGR - which roles user has
BAGROP - roles

所需的输出如下:

+----------+----------------+------------------------------------------------------------------------------------------+
| USER_KEY |   USER_NAME    |                                          ROLES                                           |
+----------+----------------+------------------------------------------------------------------------------------------+
|        1 | MIKE BAILEY    | <div>TESTING ROLE</div><div>ANOTHER ROLE</div><div>FOURTH ONE</div><div>SIXTH TEST</div> |
|        2 | GEORGE SMITH   | <div>TESTING ROLE</div><div>ANOTHER ROLE</div><div>FOURTH ONE</div><div>SIXTH TEST</div> |
|        3 | JIMMY BUSH     | <div>TESTING ROLE</div><div>ANOTHER ROLE</div><div>FOURTH ONE</div><div>SIXTH TEST</div> |
|        4 | SERGEY CLINTON | <div>TESTING ROLE</div><div>ANOTHER ROLE</div><div>FOURTH ONE</div><div>SIXTH TEST</div> |
+----------+----------------+------------------------------------------------------------------------------------------+

我已经尝试过的是使用FOR XML PATH,但它没有给我预期的结果:

SELECT U.USER_KEY, U.USER_FNM + ' ' + U.USER_LNM [USER_NAME], (SELECT '<div>' + OP.GROP_NME + '</div>' FROM BAGROP OP_1 WHERE OP_1.GROP_KEY = OP.GROP_KEY FOR XML PATH(''))
FROM BAUSER U JOIN BAUSGR GR ON U.USER_KEY = GR.USER_KEY
JOIN BAGROP OP ON GR.GROP_KEY = OP.GROP_KEY

我的主要问题是组用户密钥和用户名如何以及添加具有用户角色的列。

表创建和插入:

CREATE TABLE BAUSER
(
USER_KEY INT,
USER_FNM VARCHAR(50),
USER_LNM VARCHAR(50)
)


CREATE TABLE BAUSGR
(
USER_KEY INT,
GROP_KEY INT
)

CREATE TABLE BAGROP
(
GROP_KEY INT,
GROP_NME VARCHAR(50)
)

INSERT INTO BAGROP(GROP_KEY,GROP_NME)
VALUES(1,'FIRST ROLE')
GO
INSERT INTO BAGROP(GROP_KEY,GROP_NME)
VALUES(2,'TESTING ROLE')
GO
INSERT INTO BAGROP(GROP_KEY,GROP_NME)
VALUES(3,'ANOTHER ROLE')
GO
INSERT INTO BAGROP(GROP_KEY,GROP_NME)
VALUES(4,'FOURTH ONE')
GO
INSERT INTO BAGROP(GROP_KEY,GROP_NME)
VALUES(5,'SIXTH TEST')
GO
INSERT INTO BAGROP(GROP_KEY,GROP_NME)
VALUES(6,'SEVEN - ROLE 7')
GO

INSERT INTO BAUSER(USER_KEY,USER_FNM,USER_LNM)
VALUES(1,'MIKE','BAILEY')
GO
INSERT INTO BAUSER(USER_KEY,USER_FNM,USER_LNM)
VALUES(2,'GEORGE','SMITH')
GO
INSERT INTO BAUSER(USER_KEY,USER_FNM,USER_LNM)
VALUES(3,'JIMMY','BUSH')
GO
INSERT INTO BAUSER(USER_KEY,USER_FNM,USER_LNM)
VALUES(4,'SERGEY','CLINTON')
GO
INSERT INTO BAUSER(USER_KEY,USER_FNM,USER_LNM)
VALUES(5,'DONALD','TESTING')
GO


INSERT INTO BAUSGR(USER_KEY,GROP_KEY)
VALUES(1,2)
GO
INSERT INTO BAUSGR(USER_KEY,GROP_KEY)
VALUES(1,3)
GO
INSERT INTO BAUSGR(USER_KEY,GROP_KEY)
VALUES(1,4)
GO
INSERT INTO BAUSGR(USER_KEY,GROP_KEY)
VALUES(1,5)
GO
INSERT INTO BAUSGR(USER_KEY,GROP_KEY)
VALUES(2,1)
GO
INSERT INTO BAUSGR(USER_KEY,GROP_KEY)
VALUES(2,2)
GO
INSERT INTO BAUSGR(USER_KEY,GROP_KEY)
VALUES(2,3)
GO
INSERT INTO BAUSGR(USER_KEY,GROP_KEY)
VALUES(2,4)
GO

1 个答案:

答案 0 :(得分:1)

你能试试吗?

SELECT U.USER_KEY, U.USER_FNM + ' ' + U.USER_LNM [USER_NAME]
, ROLES= (SELECT  GROP_NME AS div 
         FROM BAUSER U1 
         JOIN BAUSGR GR1 ON U1.USER_KEY = GR1.USER_KEY
         JOIN BAGROP OP1 ON GR1.GROP_KEY = OP1.GROP_KEY         
         WHERE U.USER_KEY = U1.USER_KEY ORDER BY GROP_NME FOR XML PATH('')) 
FROM BAUSER U 
JOIN BAUSGR GR ON U.USER_KEY = GR.USER_KEY
--JOIN BAGROP OP ON GR.GROP_KEY = OP.GROP_KEY
GROUP BY U.USER_KEY, U.USER_FNM + ' ' + U.USER_LNM;

已编辑的版本(已简化,在内部查询中删除FROM BAUSER):

SELECT U.USER_KEY, U.USER_FNM + ' ' + U.USER_LNM [USER_NAME]
, ROLES= (SELECT   GROP_NME AS div   
         FROM  BAUSGR GR1  
         JOIN BAGROP OP1 ON GR1.GROP_KEY = OP1.GROP_KEY         
         WHERE U.USER_KEY = GR1.USER_KEY
          ORDER BY GROP_NME
          FOR XML PATH('') 
         ) 
FROM BAUSER U 
JOIN BAUSGR GR ON U.USER_KEY = GR.USER_KEY    
GROUP BY U.USER_KEY, U.USER_FNM + ' ' + U.USER_LNM;

输出(两个查询都相同):

+----+----------+--------------+------------------------------------------------------------------------------------------+
|    | USER_KEY |  USER_NAME   |                                          ROLES                                           |
+----+----------+--------------+------------------------------------------------------------------------------------------+
|  1 |        1 | MIKE BAILEY  | <div>ANOTHER ROLE</div><div>FOURTH ONE</div><div>SIXTH TEST</div><div>TESTING ROLE</div> |
|  2 |        2 | GEORGE SMITH | <div>ANOTHER ROLE</div><div>FIRST ROLE</div><div>FOURTH ONE</div><div>TESTING ROLE</div> |
+----+----------+--------------+------------------------------------------------------------------------------------------+