我在不返回任何值时遇到问题。数据库中有符合此条件的帐户。有些困惑为什么他们不被退回。 有什么建议吗?
select accountid from `table1`
where not in (select accountid from `table1` where action != "Action8")
答案 0 :(得分:3)
请勿使用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;
答案 1 :(得分:0)
确保您要引用的子查询结果中不希望出现的字段:
select accountid from `table1`
where accountid not in (select accountid from `table1` where action != "Action8")