从上表中,我想选择不在project_id' 1'中的user_id。
如果我使用project_id!= 1,它会删除project_id 1中的user_id。但是,如果user_id出现在另一个project_id中(如表中的用户8和10),那么这些用户将出现在结果。我怎么能避免这个?
答案 0 :(得分:0)
您可以对此
使用内部选择查询 Select * from table
where user_id not in
(Select user_id from table where project_id = 1)
答案 1 :(得分:0)
使用not in
和子查询
select *
from MyTable
where user_id not in
(select user_id
from MyTable
where project_id = 1)
或not exists
select m1.*
from MyTable m1
where not exists
(select m2.user_id
from MyTable m2
where m2.project_id = 1
and m2.user_id = m1.user_id)
答案 2 :(得分:0)
结帐是否也有帮助:使用MINUS
select user_id from mytable a
minus
select user_id from mytable b where b.project_id = 1