sql - 无法找到不在我的列表中的数据
问题描述
我有一张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')
解决方案
您需要在派生表中明确列出这些值。然后你可以使用left join
ornot 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);
推荐阅读
- c# - 为什么这个 Fixture 不止一次运行它的构造函数?
- loops - 使用 Python3,我想减少 if-elif 代码
- react-admin - update 和 updateMany previousData 参数的 react-admin 文档
- python - 数据流中的 ValueError:无效的 GCS 位置:无
- java - JBoss EAP 7.2 EJB 服务器到客户端通信,无需 http
- xml - 使用 XML::Simple 将 XML 解析为散列时如何保留前导空格
- c# - 实体配置问题 INSERT 语句与 FOREIGN KEY 约束冲突
- c# - 如何使 appsettings.json 配置在项目而不是 bin 文件夹中?
- dataframe - 如何为 DataFrame 中的每一行返回一个 DataFrame 并将结果连接到一个 DataFrame 中?
- sql - SQL:跨两列的唯一值