+---------------+
+------------+ |RolesUsers |
|Roles | |---------------| +---------------+
|+-----------| |ID | |Users |
+-+|ID |+---+|RoleID | |---------------|
| |RoleName | |UserID |+--+|ID |
| +------------+ +---------------+ |UserName |
| +---------------+
|
| +-----------------+
| |RolesMemberRoles |
| |-----------------|
| |ID |
+-+|RoleID |
+-+|MemberRoleID |
+-----------------+
PARAMETERS p_RoleName Text ( 255 ); SELECT Roles.RoleName, Users.UserName FROM (RolesUsers INNER JOIN Users ON RolesUsers.UserID = Users.ID) INNER JOIN Roles ON RolesUsers.RoleID = Roles.ID Where RolesUsers.RoleID In (Select ID from Roles where Roles.RoleName = p_RoleName) UNION SELECT Roles.RoleName, Users.UserName FROM (RolesUsers INNER JOIN Users ON RolesUsers.UserID = Users.ID) INNER JOIN Roles ON RolesUsers.RoleID = Roles.ID Where RolesUsers.RoleID IN ( SELECT RolesSubroles.SubRoleID FROM RolesSubroles INNER JOIN Roles ON RolesSubRoles.RoleID = Roles.ID where Roles.RoleName = p_RoleName);
答案 0 :(得分:1)
要实现目标1,您可以使用上面的Remou建议,使用以下查询在WHERE
子句中键入RoleNames列表。但是,删除“WHERE”子句,运行查询,然后粘贴到Excel(目标3)“快速和脏”过滤可能更有意义。
SELECT DISTINCT Roles_1.RoleName,
Users.UserName
FROM Users
INNER JOIN ((Roles
INNER JOIN (RolesMemberRoles
INNER JOIN Roles AS Roles_1
ON RolesMemberRoles.MemberRoleID = Roles_1.ID)
ON Roles.ID = RolesMemberRoles.RoleID)
INNER JOIN RoleUsers
ON Roles.ID = RoleUsers.RoleID)
ON Users.ID = RoleUsers.UserID
WHERE Roles_1.RoleName In ( "Prez", "Veep", "Staffer" )
ORDER BY Roles_1.RoleName,
Users.UserName;
要返回能够履行各种角色(目标2)的用户数,您可以运行以下命令。
SELECT InheritedRoles.RoleName,
Count(*) AS NumOfTestersAvailable
FROM (SELECT DISTINCT Roles_1.RoleName,
Users.UserName
FROM Users
INNER JOIN ((Roles
INNER JOIN (RolesMemberRoles
INNER JOIN Roles AS Roles_1
ON RolesMemberRoles.MemberRoleID =
Roles_1.ID)
ON Roles.ID = RolesMemberRoles.RoleID)
INNER JOIN RoleUsers
ON Roles.ID = RoleUsers.RoleID)
ON Users.ID = RoleUsers.UserID) AS InheritedRoles
GROUP BY InheritedRoles.RoleName
ORDER BY InheritedRoles.RoleName;
上面的SQL假设每个Role都将自己包含在RoleMemberRoles表中的成员中。您应该使用DISTINCT
,因为数据模型不会阻止单个用户拥有多个角色,因此会在每个结果集中多次出现。
答案 1 :(得分:0)
建议:在Access项目中,使用Access SQL查询电子表格数据,例如在Access表和Excel数据之间创建JOIN
,可能使用带衬里的表。