查询不属于特定组的用户? (想使用EXCEPT,但MySQL似乎不支持它)

发布于 2024-10-17 12:25:34 字数 1476 浏览 8 评论 0原文

我在用户和组之间有多对多的关系:

CREATE TABLE IF NOT EXISTS `SecurityIdentifiers` (
  `Guid` char(36) NOT NULL,
  PRIMARY KEY  (`Guid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `CaseIdUsers` (
  `Sid` char(36) NOT NULL,
  `Acl` int(11) NOT NULL,
  `FirstName` varchar(45) NOT NULL,
  `LastName` varchar(45) NOT NULL,
  `CaseID` varchar(8) NOT NULL,
  PRIMARY KEY  (`Sid`),
  UNIQUE KEY `CaseID_UNIQUE` (`CaseID`),
  KEY `fk_CaseIDUsers_AccessControlLists1` (`Acl`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `GroupMembers` (
  `User` char(36) NOT NULL,
  `Group` char(36) NOT NULL,
  PRIMARY KEY  (`User`,`Group`),
  KEY `fk_Groups_has_SecurityIdentifiers_SecurityIdentifiers1` (`User`),
  KEY `fk_Groups_has_SecurityIdentifiers_Groups1` (`Group`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `Groups` (
  `Sid` char(36) NOT NULL,
  `Acl` int(11) NOT NULL,
  `Name` varchar(45) NOT NULL,
  `Description` varchar(255) NOT NULL,
  PRIMARY KEY  (`Sid`),
  UNIQUE KEY `Name_UNIQUE` (`Name`),
  KEY `fk_Groups_Access Control Lists1` (`Acl`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

我需要找到不属于特定组的所有用户。我想做类似的事情:

SELECT CaseId FROM CaseIdUsers
EXCEPT
SELECT CaseId FROM CaseIdUsers
JOIN GroupMembers ON GroupMembers.User = CaseIdUsers.Sid
JOIN Groups ON GroupMembers.Group = Groups.Sid
WHERE Groups.Name = 'MyGroupName'

但 MySQL 不支持 EXCEPT。我现在该怎么办?

I have a many to many relationship between users and groups:

CREATE TABLE IF NOT EXISTS `SecurityIdentifiers` (
  `Guid` char(36) NOT NULL,
  PRIMARY KEY  (`Guid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `CaseIdUsers` (
  `Sid` char(36) NOT NULL,
  `Acl` int(11) NOT NULL,
  `FirstName` varchar(45) NOT NULL,
  `LastName` varchar(45) NOT NULL,
  `CaseID` varchar(8) NOT NULL,
  PRIMARY KEY  (`Sid`),
  UNIQUE KEY `CaseID_UNIQUE` (`CaseID`),
  KEY `fk_CaseIDUsers_AccessControlLists1` (`Acl`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `GroupMembers` (
  `User` char(36) NOT NULL,
  `Group` char(36) NOT NULL,
  PRIMARY KEY  (`User`,`Group`),
  KEY `fk_Groups_has_SecurityIdentifiers_SecurityIdentifiers1` (`User`),
  KEY `fk_Groups_has_SecurityIdentifiers_Groups1` (`Group`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `Groups` (
  `Sid` char(36) NOT NULL,
  `Acl` int(11) NOT NULL,
  `Name` varchar(45) NOT NULL,
  `Description` varchar(255) NOT NULL,
  PRIMARY KEY  (`Sid`),
  UNIQUE KEY `Name_UNIQUE` (`Name`),
  KEY `fk_Groups_Access Control Lists1` (`Acl`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

I need to find all the users who aren't in a specific group. I wanted to do something like:

SELECT CaseId FROM CaseIdUsers
EXCEPT
SELECT CaseId FROM CaseIdUsers
JOIN GroupMembers ON GroupMembers.User = CaseIdUsers.Sid
JOIN Groups ON GroupMembers.Group = Groups.Sid
WHERE Groups.Name = 'MyGroupName'

But MySQL doesn't support EXCEPT. What do I do now?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

演多会厌 2024-10-24 12:25:34

您可以使用 NOT IN() ,或 < ;> ANY()

SELECT CaseId
FROM CaseIdUsers
WHERE CaseID NOT IN (
  SELECT CaseId 
  FROM CaseIdUsers
    JOIN GroupMembers ON GroupMembers.User = CaseIdUsers.Sid
    JOIN Groups ON GroupMembers.Group = Groups.Sid
  WHERE Groups.Name = 'MyGroupName'
)

或者,您可以将 LEFT JOINGROUP BY 结合使用

SELECT CaseId 
FROM CaseIdUsers
  LEFT JOIN GroupMembers ON GroupMembers.User = CaseIdUsers.Sid
  LEFT JOIN Groups ON GroupMembers.Group = Groups.Sid
    AND Groups.Name = 'MyGroupName'
HAVING COUNT(Groups.Sid) = 0

You can use a NOT IN(), or a <> ANY()

SELECT CaseId
FROM CaseIdUsers
WHERE CaseID NOT IN (
  SELECT CaseId 
  FROM CaseIdUsers
    JOIN GroupMembers ON GroupMembers.User = CaseIdUsers.Sid
    JOIN Groups ON GroupMembers.Group = Groups.Sid
  WHERE Groups.Name = 'MyGroupName'
)

Alternatively, you can use a LEFT JOIN with a GROUP BY

SELECT CaseId 
FROM CaseIdUsers
  LEFT JOIN GroupMembers ON GroupMembers.User = CaseIdUsers.Sid
  LEFT JOIN Groups ON GroupMembers.Group = Groups.Sid
    AND Groups.Name = 'MyGroupName'
HAVING COUNT(Groups.Sid) = 0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文