Sql server SQL Server-如何检查两个项目是否与另一组项目具有相同的关系?

Sql server SQL Server-如何检查两个项目是否与另一组项目具有相同的关系?,sql-server,relation,Sql Server,Relation,我有一张与员工的桌子TBlemEmployee: | ID | Name | | 1 | Smith | | 2 | Black | | 3 | Thompson | 以及一个包含角色tblRoles的表: | ID | Name | | 1 | Submitter | | 2 | Receiver | | 3 | Analyzer | 我还有一个表,其中列出了员工与其角色之间的关系,该表具有多对多关系类型tblEmployeeRoleR

我有一张与员工的桌子TBlemEmployee:

| ID | Name      |
| 1  | Smith     |
| 2  | Black     |
| 3  | Thompson  |
以及一个包含角色tblRoles的表:

| ID | Name      |
| 1  | Submitter |
| 2  | Receiver  |
| 3  | Analyzer  |
我还有一个表,其中列出了员工与其角色之间的关系,该表具有多对多关系类型tblEmployeeRoleRel:

| EmployeeID | RoleID  |
| 1          | 1       |
| 1          | 2       |
| 2          | 1       |
| 2          | 2       |
| 2          | 3       |
| 3          | 3       |

我需要从Tblemployeer中选择ID和Name,它们与ID=1的员工在tblEmployeeRoleRel中的角色集完全相同。如何操作?

使用where子句将您正在查看的角色限制为employeeID为1的角色,并使用having子句确保员工的角色数与employee1的角色数匹配

SELECT A.EmployeeID 
FROM tblEmployeeRoleRel A
WHERE Exists (SELECT 1 
              FROM tblEmployeeRoleRel B
              WHERE B.EmployeeID = 1
                and B.RoleID = A.RoleID)
GROUP BY A.EmployeeID
HAVING count(A.RoleID) = (SELECT count(C.RoleID) 
                          FROM tblEmployeeRoleRel C 
                          WHERE EmployeeID = 1)

这假设employeeID和roleID在tblEmployeeRoleRel中是唯一的,否则我们可能必须区分上面的roleID字段。

使用where子句将您正在查看的角色限制为employeeID为1的角色,并使用having子句确保员工的角色计数与employee1的角色计数匹配

SELECT A.EmployeeID 
FROM tblEmployeeRoleRel A
WHERE Exists (SELECT 1 
              FROM tblEmployeeRoleRel B
              WHERE B.EmployeeID = 1
                and B.RoleID = A.RoleID)
GROUP BY A.EmployeeID
HAVING count(A.RoleID) = (SELECT count(C.RoleID) 
                          FROM tblEmployeeRoleRel C 
                          WHERE EmployeeID = 1)
Declare @EmployeeID int = 1 -- change this to whatever employee ID you like, or perhaps you'd pass an Employee ID to it in a stored procedure.
Select Distinct e.EmployeeID  -- normally distinct would incur extra overhead, but in this case you only want the employee IDs. not using Distinct when an employee has multiple roles will give you multiple employee IDs.
from tblEmployeeRoleRel as E 
where E.EmployeeID not in
(Select EmployeeID from tblEmployeeRoleRel where RoleID not in (Select RoleID from tblEmployeeRoleRel where Employee_ID = @EmployeeID)) 
and exists (Select EmployeeID from tblEmployeeRoleRel where EmployeeID = e.EmployeeID)  -- removes any "null" matches.
and E.Employee_ID <> @Employee_ID  -- this keeps the employee ID itself from matching.

这假设employeeID和roleID在tblEmployeeRoleRel中是唯一的,否则我们可能必须区分上面的roleID字段。

请更具体一些请更具体一些请它们是唯一的。这个解决方案对我有效,但它会返回额外的带有count的列,因此我不能将其用作子查询,例如…在EployeeID中…您可以从select中删除countA.RoleID,它只是为了显示其唯一性。这个解决方案适合我,但它会返回额外的带有count的列,因此我不能将其用作子查询,例如…如果eployeid在…您可以从select中删除countA.RoleID,它只是用来显示总数
Declare @EmployeeID int = 1 -- change this to whatever employee ID you like, or perhaps you'd pass an Employee ID to it in a stored procedure.
Select Distinct e.EmployeeID  -- normally distinct would incur extra overhead, but in this case you only want the employee IDs. not using Distinct when an employee has multiple roles will give you multiple employee IDs.
from tblEmployeeRoleRel as E 
where E.EmployeeID not in
(Select EmployeeID from tblEmployeeRoleRel where RoleID not in (Select RoleID from tblEmployeeRoleRel where Employee_ID = @EmployeeID)) 
and exists (Select EmployeeID from tblEmployeeRoleRel where EmployeeID = e.EmployeeID)  -- removes any "null" matches.
and E.Employee_ID <> @Employee_ID  -- this keeps the employee ID itself from matching.