2017-01-22 9 views
0

MySqlクエリを作成して、テーブル内に見つからない行を特定するのが難しいです。同じテーブルの複数のフィールドを選択しない

テーブルT構造は以下の通りである:

+++++++++++++++++++++++++++++++++++++++++++++++ 
+ Unique ID + Group + Key1 + Key2 + Value + 
+++++++++++++++++++++++++++++++++++++++++++++++ 
+ 34  + A  + d1  + e2  + 123 + 
+ 35  + A  + d1  + e3  + 456 + 
+ 36  + A  + d1  + e1  + 444 + 
+ 37  + A  + d2  + e3  + 555 + 
+ 38  + B  + d1  + e3  + 555 + 
+ 39  + B  + d3  + e2  + 111 + 
+ ...  + ... + ... + ... + ... + 
+++++++++++++++++++++++++++++++++++++++++++++++ 

行はラベルABでグループ化されています。グループAではグループBではなく、Key1Key2を考慮して行のセットを特定する必要があります。 Unique IDのみが表内で一意です。言い換えれば

、クエリが返す必要があります:

+++++++++++++++++++++++++++++++++++++++++++++++ 
+ Unique ID + Group + Key1 + Key2 + Value + 
+++++++++++++++++++++++++++++++++++++++++++++++ 
+ 34  + A  + d1  + e2  + 123 + 
+ 36  + A  + d1  + e1  + 444 + 
+ 37  + A  + d2  + e3  + 555 + 
+++++++++++++++++++++++++++++++++++++++++++++++ 

答えて

1

私はnot existsを使用します。多くのデータベースが複数列inをサポートしていないという理由だけで、私はnot existsを好む

select ta.* 
from t ta 
where ta.group = 'A' and 
     (ta.key1, ta.key2) not in (select tb.key1, tb.key2 from t tb where tb.group = 'B'); 

select ta.* 
from t ta 
where ta.group = 'A' and 
     not exists (select 1 
        from t tb 
        where tb.group = 'B' and tb.key1 = ta.key1 and tb.key2 = ta.key2 
       ); 

は、MySQLでは、あなたはまた、複数列 inを使用することができます。

関連する問題