不要使用not in
。从语义上讲,这是违反直觉的。如果子查询中的 任何 值为NULL
,则不返回任何行。
使用not exists
代替;
select t1.accountid
from `table1` t1
where not exists (select 1
from table1 tt1
where tt1.accountid = t1.accountid and
tt1.action <> 'Action8'
);
或使用group by
和having
:
select t1.accountid
from table1 t1
group by t1.accountid
having sum(case when action = 'Action8' then 1 else 0 end) = 0;