首页 > 解决方案 > 无法找到不在我的列表中的数据

问题描述

我有一张fruits这样的桌子

水果
苹果
香蕉

我有这个清单('apple', 'banana', 'mango', 'apple', 'orange')。我想看看哪些水果不在 mufruits表中。所以我希望得到如下结果:

不在列表中的水果
芒果
SELECT fruit as "fruits not in list"
FROM fruits
WHERE fruit not in ('apple', 'banana', 'mango', 'apple', 'orange')

标签: sqldatabaseoracleselect

解决方案


您需要在派生表中明确列出这些值。然后你可以使用left joinornot exists或类似的方法:

select f.fruit
from (select 'apple' as fruit from dual union all
      select 'banana' from dual union all
      select 'mango' from dual union all
      select 'apple' from dual union all
      select 'orange' from dual union all
     ) f
where not exists (select 1 from fruits f2 where f2.fruit = f.fruit);

推荐阅读