MySql Select语句(其他公司)

时间:2020-02-04 14:41:58

标签: mysql sql distinct

我试图选择为其他公司列出但不在我公司中列出的不同用户(1)。这是一个例子

Placement User Company
1          1      1
2          1      2
3          2      2
4          3      1
5          2      1

从此表中,我想获得第4行,因为他在其他公司(不是1)中,但在其他公司中列出。我不希望其他人,因为他们同时在我的公司和其他人中列出。有人可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

您可以使用NOT IN。例如:

select distinct user 
from t 
where user not in (
  select user from t where company = 1
)

答案 1 :(得分:0)

我认为这是您想要的逻辑:

select t.*
from mytable t
where not exists (
    select 1 from mytable t1 where t1.user = t.user and t1.company = 1
)

这将为您提供相同用户和公司1的其他记录不存在的记录。