SQL INNER JOIN + SELECT NOT EXIST ID's
SQL INNER JOIN + SELECT NOT EXIST ID's
我有三个 table:
Table一:用户
Table二:角色
Table三: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
我需要 Select 所有 UsersID
不在 table UserInRoles
来自 table Users
我试过了:
SELECT DISTINCT Users.UserId, Users.FullName
FROM Users
INNER JOIN UserInRoles
ON Users.UserId <> UserInRoles.UserId
认为 NOT IN
或 NOT EXISTS
:
select u.*
from users u
where not exists (select 1 from UserInRules ur where u.UserId = ur.UserId);
对于 Select 所有不在 table UserInRoles 中的用户 ID 来自 table 用户,只需使用 not in
select distinct * from users where userid not in
(select userid from UserInroles)
我有三个 table:
Table一:用户
Table二:角色
Table三: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
我需要 Select 所有 UsersID
不在 table UserInRoles
来自 table Users
我试过了:
SELECT DISTINCT Users.UserId, Users.FullName
FROM Users
INNER JOIN UserInRoles
ON Users.UserId <> UserInRoles.UserId
认为 NOT IN
或 NOT EXISTS
:
select u.*
from users u
where not exists (select 1 from UserInRules ur where u.UserId = ur.UserId);
对于 Select 所有不在 table UserInRoles 中的用户 ID 来自 table 用户,只需使用 not in
select distinct * from users where userid not in
(select userid from UserInroles)