可能这很奇怪,但我需要将ActionId的值连接到对应的roleId组,而ActionID是必须的。有些像
ActionID RoleId
"1357" 1
"2468" 2
以下是我目前的情况,我正在寻找MS SQL中的GROUP_CONCAT等价物。
select av.ActionId, ra.RoleId from RoleAction ra join ActionValue av
on ra.ActionId = av.ActionId order by av.ActionId
ActionID RoleId
1 1
3 1
5 1
7 1
4 2
2 2
6 2
8 2
有办法吗?提前谢谢。
答案 0 :(得分:1)
这应该有效:
WITH CTE_A AS
(
select av.ActionId, ra.RoleId from RoleAction ra join ActionValue av
on ra.ActionId = av.ActionId
)
SELECT DISTINCT A.RoleId,
(SELECT '' +
CAST(B.ActionId AS varchar(10))
FROM CTE_A B
WHERE B.RoleID = A.RoleID
FOR XML PATH('')) AS ActionID
FROM CTE_A A
GROUP BY A.RoleID
答案 1 :(得分:1)
您可以使用FOR XML PATH('')
和内部查询使其正常工作:
SELECT DISTINCT T1.RoleID,
(SELECT '' + ActionID
FROM RoleAction T2
WHERE T1.RoleID = T2.RoleID
ORDER BY ActionID
FOR XML PATH(''))
FROM RoleAction T1