我们有 2 张桌子:
UserTable
userid username
1 test
2 test2
3 test3
UserStatus
statusid userid status
1 1 1
2 1 3
3 1 7
4 2 1
现在我们需要一个用户列表,其中包含没有状态 3 的用户。
有什么想法吗?
您可以使用NOT EXISTS
:
select u.userid,
u.username
from usertable u
where not exists (select s.userid
from userstatus s
where s.status = 3
and u.userid = s.userid);
您可以使用 NOT IN:
SELECT usertable.*
FROM usertable
WHERE userid NOT IN (SELECT userid from userstatus where status=3)
请在此处查看小提琴。
SELECT * FROM UserTable
WHERE userid NOT IN
(SELECT userid FROM UserStatus WHERE status = 3)
SELECT username from UserTable
WHERE userid NOT IN (SELECT userid FROM UserStatus WHERE status = 3)