0

SOでこれを見つけようとしました。

私はテーブルを持っています、

id   | col2 | col3
----   ----   ----
5     smith   (null)
5     smith  100
12    Jackson 356
12    Jackson 400
8     Johnson (null)
9     bob     1200

このシナリオでは、同じ ID に null 以外の行が 1 つしかないセットの行のみが必要です。つまり、スミスもジョンソンもいらない。ジャクソンとボブだけが欲しい。

私が試してみました、

select * from table 
where is not null a
nd not exists (select * from table where is null)

私はそれを働かせることができません。

4

2 に答える 2

1

使用できますNOT EXISTSが、各 ID を参照するために WHERE を含めます。

select * 
from yourtable t
where col3 is not null 
  and not exists (select id 
                  from yourtable d
                  where d.col3 is null
                   and t.id = d.id);

デモを見る

于 2013-08-12T18:10:30.703 に答える