查询不属于特定组的用户? (想使用EXCEPT,但MySQL似乎不支持它)
我在用户和组之间有多对多的关系:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
NOT IN()
,或< ;> ANY()
或者,您可以将
LEFT JOIN
与GROUP BY
结合使用You can use a
NOT IN()
, or a<> ANY()
Alternatively, you can use a
LEFT JOIN
with aGROUP BY