Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何使用stuff()列出每个应用程序的用户角色?_Sql_Sql Server_Tsql - Fatal编程技术网

Sql 如何使用stuff()列出每个应用程序的用户角色?

Sql 如何使用stuff()列出每个应用程序的用户角色?,sql,sql-server,tsql,Sql,Sql Server,Tsql,考虑下表用户 application user_id user_role role_permissions ------------------------------------------------------- 1 20 A add 1 20 B edit 1 30 A add 1

考虑下表
用户

application  user_id    user_role     role_permissions
-------------------------------------------------------
1            20          A             add
1            20          B             edit
1            30          A             add
1            20          B             edit
1            30          V             delete
1            20          G             duplicate
1            40          X             purge
2            20          W             hide
2            20          P             unhide
2            40          X             purge
我想列出每个用户在每个应用程序中拥有的所有角色,如下所示:

application  user_id    roles       permissions
-----------------------------------------------
1            20         A;B;G       add;edit;duplicate
1            30         A;V         add;delete
1            40         X           purge
2            20         W;P         hide;unhide
2            40         X           purge
我一直在尝试使用
东西
,但没有成功:

select users.user_id,
       stuff
       ((
           select ';' + user_role from users a
           where  a.user_id = b.user_id
           for xml path('')),1,1,'' 
       ) as roles
 from users b
这给了我:

user_id  permissions
-----------------------
20       A;B;B;G
20       A;B;B;G
20       A;B;B;G
30       A;V
30       A;V
...
etc
如何获取每个应用程序的不同行?

在外部选择之前使用
不同的

select u.user_id,
       stuff( (select distinct';' + user_role
               from users u2
               where u.user_id = u2.user_id and u.application = u2.application
               for xml path('')
              ), 1, 1, '' 
            ) as roles
from (select distinct user_id, application from users u) u;
我不知道obj_id是什么。明显的相关条件在外部选择之前使用
user\u id

使用
distinct

select u.user_id,
       stuff( (select distinct';' + user_role
               from users u2
               where u.user_id = u2.user_id and u.application = u2.application
               for xml path('')
              ), 1, 1, '' 
            ) as roles
from (select distinct user_id, application from users u) u;

我不知道obj_id是什么。明显的相关条件使用
用户id

如果您想要在结果中包含四列,我认为您需要这样的内容:

select
    u.application,
    u.user_id,
    stuff( (select distinct';' + user_role
           from users u2
           where  u.user_id = u2.user_id
           and u.application = u3.application
           for xml path('')
          ), 1, 1, '' 
        ) as roles
    stuff( (select distinct';' + role_permissions
           from users u3
           where  u.user_id = u3.user_id
           and u.application = u3.application
           for xml path('')
          ), 1, 1, '' 
        ) as permissions
from (select distinct application, user_id from users u) u;

如果您希望在结果中包含四列,我认为您需要这样的内容:

select
    u.application,
    u.user_id,
    stuff( (select distinct';' + user_role
           from users u2
           where  u.user_id = u2.user_id
           and u.application = u3.application
           for xml path('')
          ), 1, 1, '' 
        ) as roles
    stuff( (select distinct';' + role_permissions
           from users u3
           where  u.user_id = u3.user_id
           and u.application = u3.application
           for xml path('')
          ), 1, 1, '' 
        ) as permissions
from (select distinct application, user_id from users u) u;

正如另一个人向我指出的,
STUFF()
并不是将不同行中的值放在一列中的真正方法,
STUFF()
处理连接,而
FOR XML Path
处理所有数据传输。在我看来,它工作得很好。对于用户中的每一行,您都将获得其权限。也许你只想要不同的行?@RyanWilson实际上,stuff并没有连接任何东西。我们使用
Stuff
删除第一个
字符。@ZoharPeled“STUFF函数将一个字符串插入另一个字符串。它在起始位置删除第一个字符串中指定长度的字符,然后在起始位置将第二个字符串插入第一个字符串中。”对我来说,这听起来像是某种连接。()@Pr0no为什么表中有重复的记录?正如另一个人向我指出的,
STUFF()
不是将不同行中的值放在一列中的真正原因,
STUFF()
处理连接,而XML路径的
负责所有数据的传输。在我看来,它工作得很好。对于用户中的每一行,您都将获得其权限。也许你只想要不同的行?@RyanWilson实际上,stuff并没有连接任何东西。我们使用
Stuff
删除第一个
字符。@ZoharPeled“STUFF函数将一个字符串插入另一个字符串。它在起始位置删除第一个字符串中指定长度的字符,然后在起始位置将第二个字符串插入第一个字符串中。”对我来说,这听起来像是某种连接。()@Pr0no为什么表中有重复记录?请注意,我添加了每个应用程序权限的复杂性。这将如何改变您的建议?已将其更新为包括应用程序。请注意,我已添加了每个应用程序权限的复杂性。这将如何改变您的建议?将其更新为包括应用程序。我的坏,更改了:)请注意,我现在添加了每个应用程序需要角色的复杂性(最初忘记提到这一点)。请帮助我理解在您的建议中是如何实现的?基本上,您需要将应用程序id添加到select distinct查询,以及子查询的where子句上的条件-`and u.application_id=u2.application_id`@Pr0no。这是对查询的一个非常小的更改。我的错误,更改了:)请注意,我现在增加了每个应用程序需要角色的复杂性(最初忘记提到这一点)。请帮助我理解在您的建议中是如何实现的?基本上,您需要将应用程序id添加到select distinct查询,以及子查询的where子句上的条件-`and u.application_id=u2.application_id`@Pr0no。这是对查询的一个非常小的更改。