MySql Select 声明(其他公司)
MySql Select Statement (Other Company)
我正在尝试 select 为其他公司列出但不在我公司 (1) 中的不同用户。这是一个例子
Placement User Company
1 1 1
2 1 2
3 2 2
4 3 1
5 2 1
从这个 table,我想得到第 4 行,因为他在其他公司(不是 1)但列在其他公司。我不想要其他人,因为他们在我的公司和其他公司都上市。任何人都可以帮忙吗?
我认为这就是你想要的逻辑:
select t.*
from mytable t
where not exists (
select 1 from mytable t1 where t1.user = t.user and t1.company = 1
)
这为您提供了同一用户和公司 1 没有其他记录的记录。
您可以使用 NOT IN
。例如:
select distinct user
from t
where user not in (
select user from t where company = 1
)
我正在尝试 select 为其他公司列出但不在我公司 (1) 中的不同用户。这是一个例子
Placement User Company
1 1 1
2 1 2
3 2 2
4 3 1
5 2 1
从这个 table,我想得到第 4 行,因为他在其他公司(不是 1)但列在其他公司。我不想要其他人,因为他们在我的公司和其他公司都上市。任何人都可以帮忙吗?
我认为这就是你想要的逻辑:
select t.*
from mytable t
where not exists (
select 1 from mytable t1 where t1.user = t.user and t1.company = 1
)
这为您提供了同一用户和公司 1 没有其他记录的记录。
您可以使用 NOT IN
。例如:
select distinct user
from t
where user not in (
select user from t where company = 1
)