2017-01-16 3 views
2

enter image description here使用方法!=重複したエントリがある場合は?

上記の表から、project_id '1'にな​​いuser_idを選択します。

project_id!= 1を使用すると、project_id 1のuser_idが削除されます。ただし、user_idが別のproject_idに存在する場合(ユーザーの8と10のように)、ユーザーは結果に表示されます。どうすればこれを避けることができますか?それはまた、有用である場合は、この

Select * from table 
     where user_id not in 
    (Select user_id from table where project_id = 1) 
+0

つまり、user_idがproject_id = 1に設定されていない行が必要ですか?存在しないことはあなたが望むものです。 – jarlh

+0

'select user_id テーブル user_idがどこにありません(どこからproject_id = 1のテーブルからuser_idを選択してください) ' –

答えて

0

:使用MINUS

select user_id from mytable a 
minus 
select user_id from mytable b where b.project_id = 1 
+0

@JohnHCありがとうございました –

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) 
+0

サブクエリからuser_id NULLが返されたらどうなりますか? – jarlh

+0

@jarlh良い点... – JohnHC

0

Checkoutに内部選択クエリを使用することができます

関連する問題