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);
SQL FiddlewithDemoを参照してください
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)