我有三张桌子:
表一:用户
表二:角色
表三:UserInRoles
UserID | FullName
--------------------------------------------------
07DCEE4A-6598-42E1-95C6-0390FF8BB534 | John Doe
RoleID
---------------------------------------
E5C46F8E-EE6A-4052-AABA-08184E5F0158
UserID | RoleID
---------------------------------------------------------------------------
07DCEE4A-6598-42E1-95C6-0390FF8BB534 | E5C46F8E-EE6A-4052-AABA-08184E5F0158
我需要从表UsersID
中选择不在表UserInRoles
中的所有Users
我试过:
SELECT DISTINCT Users.UserId, Users.FullName
FROM Users
INNER JOIN UserInRoles
ON Users.UserId <> UserInRoles.UserId
答案 0 :(得分:4)
思考NOT IN
或NOT EXISTS
:
select u.*
from users u
where not exists (select 1 from UserInRules ur where u.UserId = ur.UserId);
答案 1 :(得分:0)
要从表Users中选择不在表UserInRoles中的所有UsersID,只需使用not in
select distinct * from users where userid not in
(select userid from UserInroles)