SQL选择语句具有用户、角色和权限

时间:2021-09-15 23:40:14

Lets say that I have tables:

假设我有表格:

  1. Users
  2. 用户
  3. Users_in_Roles
  4. Users_in_Roles
  5. Roles
  6. 角色
  7. Rights_in_Roles
  8. Rights_in_Roles
  9. Rights
  10. 权利

Keys are standard( UserFk, RoleFk, RightFk)

键是标准的(UserFk, RoleFk, RightFk)

The question is: how to get all users that are in role with right X (id = 100)

问题是:如何让所有的用户都使用正确的X (id = 100)

I Have no idea how to touch this problem. Please help and sorry for my english.

我不知道如何处理这个问题。请为我的英语说声抱歉。

SELECT [dbo].[System_Users].[Id]
  ,[UserName]
  ,[FirstName]
  ,[LastName]
  ,[Email]
  ,RoleFk
  ,[dbo].[System_Roles].Name
  FROM [dbo].[System_Users]
INNER JOIN [dbo].[System_Roles_System_Users]
ON [dbo].[System_Roles_System_Users].UserFk = [dbo].[System_Users].Id
INNER JOIN [dbo].[System_Roles]
ON [dbo].[System_Roles].Id = [dbo].[System_Roles_System_Users].RoleFk

WHERE ?

I tryied sth like that, could you tell me what iw wrong?

我很喜欢这样,你能告诉我什么是错的吗?

SELECT 
DISTINCT System_Users.Id,
System_Users.FullName
FROM System_Users
INNER JOIN Dict_Rights_System_Users
ON System_Users.Id = Dict_Rights_System_Users.UserFk
INNER JOIN System_Roles_System_Users
ON System_Roles_System_Users.UserFk = System_Users.Id
WHERE
RightFk = 136
OR
136 IN (SELECT Dict_Rights_System_Roles.RightFk FROM Dict_Rights_System_Roles WHERE             
Dict_Rights_System_Roles.RoleFk = System_Roles_System_Users.RoleFk)
ORDER BY System_Users.FullName ASC

2 个解决方案

#1


2  

You can try with this:

你可以试试这个:

SELECT  *
FROM    Users u
WHERE   EXISTS (
        SELECT  ur.RoleFk
        FROM    Users_in_Roles ur
        WHERE   u.UserPk = ur.UserFk
        AND     EXISTS
                (
                    SELECT  1
                    FROM    Rights_in_Roles rr
                    WHERE   rr.RoleFk = ur.RoleFk
                    AND     rr.RightFk = 100
                )
)
OR  EXISTS (

        SELECT  1
        FROM    Users_Rights uri
        WHERE   u.UserPk = uri.UserFk 
        AND     uri.RightFk = 100
)

Note that the above query doesn't return RoleFk and Name for the role.

注意,上面的查询不会返回RoleFk和角色的名称。

Another approach would be:

另一种方法是:

SELECT   u.Id
        ,u.UserName
        ,u.FirstName
        ,u.LastName
        ,u.Email
        ,rr.RoleFk
        ,r.Name
FROM    Users u

        -- get users that are in role that has right
LEFT JOIN   
        Users_in_Roles ur ON
        ur.UserFk = u.UserPk
LEFT JOIN   
        Rights_in_Roles rr ON
        rr.RoleFk = ur.RoleFk
AND     rr.RightFk = 100
LEFT JOIN   
        Rights r ON
        r.RolePk = rr.RoleFk

        -- get users that have a right granted to them directly
LEFT JOIN
        Users_Rights uri ON
        u.UserPk = uri.UserFk 
AND     uri.RightFk = 100

WHERE   rr.RoleFk IS NOT NULL OR uri.UserFk IS NOT NULL

#2


2  

You will need to JOIN the tables on the key relationships. The basic structure will be:

您将需要加入关键关系的表。基本结构是:

select u.Id, 
  u.UserName,
  u.FirstName,
  u.LastName,
  u.Email,
  r.RoleFk,
  r.Name RoleName,
  rt.Name RightName
from users u
inner join users_in_roles ur
  on u.id = ur.userfk
inner join roles r
  on ur.rolefk = r.id
inner join rights_in_roles rr
  on r.rolefk = rr.rolefk
inner join rights rt
  on rr.rightfk = rt.id
where rt.id = 100

If you need help learning JOIN syntax here is a great reference:

如果你需要学习连接语法,这里有一个很好的参考:

#1


2  

You can try with this:

你可以试试这个:

SELECT  *
FROM    Users u
WHERE   EXISTS (
        SELECT  ur.RoleFk
        FROM    Users_in_Roles ur
        WHERE   u.UserPk = ur.UserFk
        AND     EXISTS
                (
                    SELECT  1
                    FROM    Rights_in_Roles rr
                    WHERE   rr.RoleFk = ur.RoleFk
                    AND     rr.RightFk = 100
                )
)
OR  EXISTS (

        SELECT  1
        FROM    Users_Rights uri
        WHERE   u.UserPk = uri.UserFk 
        AND     uri.RightFk = 100
)

Note that the above query doesn't return RoleFk and Name for the role.

注意,上面的查询不会返回RoleFk和角色的名称。

Another approach would be:

另一种方法是:

SELECT   u.Id
        ,u.UserName
        ,u.FirstName
        ,u.LastName
        ,u.Email
        ,rr.RoleFk
        ,r.Name
FROM    Users u

        -- get users that are in role that has right
LEFT JOIN   
        Users_in_Roles ur ON
        ur.UserFk = u.UserPk
LEFT JOIN   
        Rights_in_Roles rr ON
        rr.RoleFk = ur.RoleFk
AND     rr.RightFk = 100
LEFT JOIN   
        Rights r ON
        r.RolePk = rr.RoleFk

        -- get users that have a right granted to them directly
LEFT JOIN
        Users_Rights uri ON
        u.UserPk = uri.UserFk 
AND     uri.RightFk = 100

WHERE   rr.RoleFk IS NOT NULL OR uri.UserFk IS NOT NULL

#2


2  

You will need to JOIN the tables on the key relationships. The basic structure will be:

您将需要加入关键关系的表。基本结构是:

select u.Id, 
  u.UserName,
  u.FirstName,
  u.LastName,
  u.Email,
  r.RoleFk,
  r.Name RoleName,
  rt.Name RightName
from users u
inner join users_in_roles ur
  on u.id = ur.userfk
inner join roles r
  on ur.rolefk = r.id
inner join rights_in_roles rr
  on r.rolefk = rr.rolefk
inner join rights rt
  on rr.rightfk = rt.id
where rt.id = 100

If you need help learning JOIN syntax here is a great reference:

如果你需要学习连接语法,这里有一个很好的参考: