好的,我认为您需要上述规范的表和查询吗?
我从一台计算机被“分配”给一个给定的用户的规范中得到,但是可以共享吗?
计算机(int userid)
看看这个,如果你想改变任何规格请告诉我。
DECLARE @Users TABLE(
UserID INT
)
DECLARE @Computers TABLE(
ComputerID INT,
UserID INT
)
DECLARE @Groups TABLE(
GroupID INT
)
DECLARE @GroupMemberships TABLE(
UserID INT,
GroupID INT,
IsSharing INT
)
INSERT INTO @Users (UserID) SELECT 1
INSERT INTO @Users (UserID) SELECT 2
INSERT INTO @Computers (ComputerID, UserID) SELECT 1, 1
INSERT INTO @Computers (ComputerID, UserID) SELECT 2, 1
INSERT INTO @Computers (ComputerID, UserID) SELECT 3, 1
INSERT INTO @Computers (ComputerID, UserID) SELECT 4, 2
INSERT INTO @Computers (ComputerID, UserID) SELECT 5, 2
INSERT INTO @Groups (GroupID) SELECT 1
INSERT INTO @Groups (GroupID) SELECT 2
INSERT INTO @Groups (GroupID) SELECT 3
INSERT INTO @GroupMemberships (UserID,GroupID,IsSharing) SELECT 1, 1, 0
INSERT INTO @GroupMemberships (UserID,GroupID,IsSharing) SELECT 1, 2, 1
INSERT INTO @GroupMemberships (UserID,GroupID,IsSharing) SELECT 2, 2, 0
INSERT INTO @GroupMemberships (UserID,GroupID,IsSharing) SELECT 2, 3, 0
DECLARE @UserID INT
--SELECT @UserID = 1
SELECT @UserID = 2
SELECT DISTINCT
ComputerID
FROM @Computers
WHERE UserID = @UserID
UNION
SELECT DISTINCT
ComputerID
FROM @Computers c INNER JOIN
(
SELECT DISTINCT
gm.UserID
FROM @GroupMemberships gm INNER JOIN
@GroupMemberships ThisUserGroups ON gm.GroupID = ThisUserGroups.GroupID
AND ThisUserGroups.UserID = @UserID
WHERE gm.UserID != @UserID
AND gm.IsSharing = 1
) OtherUsersInSharedGroups ON c.UserID = OtherUsersInSharedGroups.UserID