SQL 子查询获取其他列值不存在的列的所有行

SQL subquery to get all rows of columns where other column value does not exist

我有以下 Accounts_Users table:

account_id | user_id
--------------------
1          | 60
2          | 60
3          | 60
1          | 50
3          | 50 
3          | 40 

我想检索所有 user_id's 没有某些行的 account_id 例如,如果 account_id = 2 我希望结果应该是:

user_id
-------
50 
40 


Since user_id = 60 have record with account = 2.

如何使用子查询或任何其他方式来实现?

子查询方式:

select distinct user_id
from accounts_users
where user_id not in (
    select user_id
    from accounts_users
    where account_id = 2)

加盟方式:

select distinct a.user_id
from accounts_users a
left join accounts_users b on b.user_id = a.user_id
    and b.account_id = 2
where b.user_id is null

您使用了子查询:

select distinct user_id
from Accounts_Users
where user_id not in (
  select user_id from Accounts_Users where account_id = 2
)