SQL 服务器 - 如何检查两个项目是否与另一组项目具有相同的关系?
SQL Server - How to check if two items have the same relations to another set of items?
我有 table 员工 (tblEmployee):
| ID | Name |
| 1 | Smith |
| 2 | Black |
| 3 | Thompson |
还有一个 table 角色 (tblRoles):
| ID | Name |
| 1 | Submitter |
| 2 | Receiver |
| 3 | Analyzer |
我还有一个 table 员工与其角色的关系,关系类型为多对多 (tblEmployeeRoleRel):
| EmployeeID | RoleID |
| 1 | 1 |
| 1 | 2 |
| 2 | 1 |
| 2 | 2 |
| 2 | 3 |
| 3 | 3 |
我需要 select 来自 tblEmployee 的 ID、名称与来自 tblEmployeeRoleRel 的角色与 ID = 1 的员工完全相同。我该怎么做?
使用 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 字段。
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.
我有 table 员工 (tblEmployee):
| ID | Name |
| 1 | Smith |
| 2 | Black |
| 3 | Thompson |
还有一个 table 角色 (tblRoles):
| ID | Name |
| 1 | Submitter |
| 2 | Receiver |
| 3 | Analyzer |
我还有一个 table 员工与其角色的关系,关系类型为多对多 (tblEmployeeRoleRel):
| EmployeeID | RoleID |
| 1 | 1 |
| 1 | 2 |
| 2 | 1 |
| 2 | 2 |
| 2 | 3 |
| 3 | 3 |
我需要 select 来自 tblEmployee 的 ID、名称与来自 tblEmployeeRoleRel 的角色与 ID = 1 的员工完全相同。我该怎么做?
使用 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 字段。
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.