sql - 从一列中选择一个不同的变量
问题描述
这是我的桌子的样本:
Order_id Item_id Payment_type
2345 120 Cash
2345 121 Cash
3346 122 Cash
3346 123 Check
3346 124 Check
4456 125 Check
4456 126 Check
5456 127 Cash
一份订单可以有一件或多件商品和一种或多种付款方式。但在我的结果中,我想要只有现金作为付款类型的订单 ID。所以在上表中,我的结果应该只有 2345 和 5456。
我试过了
Select order_id
from orders
where (payment_type = 'Cash' and payment_type <> 'Check')
但结果是 2345、3346 和 5456。
我不希望结果中出现 3346,因为它有现金和支票支付类型。
解决方案
您可以使用 MINUS 集合运算符(请参阅文档)。测试表:
create table T as
select 2345 Order_id, 120 Item_id, 'Cash' Payment_type from dual union all
select 2345, 121, 'Cash' from dual union all
select 3346, 122, 'Cash' from dual union all
select 3346, 123, 'Check' from dual union all
select 3346, 124, 'Check' from dual union all
select 4456, 125, 'Check' from dual union all
select 4456, 126, 'Check' from dual union all
select 5456, 127, 'Cash' from dual;
询问
select order_id
from T
minus
select order_id
from T
where payment_type = 'Check'
;
--result
ORDER_ID
2345
5456
Dbfiddle在这里。
推荐阅读
- parsing - 解析器测试方法和工具
- php - 多对多关系的原则约束
- javascript - 如何更新状态数组?
- spring-boot - 在 Swagger UI 中显示模型属性的默认值
- firebase - 用于颤振的 Firebase_database 插件不起作用(解决 android.arch.core:common:1.1.1。)
- xslt - xspec:预期失败和结果相同
- javascript - 如何使用 Sentry 捕获 *all* 错误和异常?
- r - 从物种生死表计算总系统发育分支长度的总和
- typescript - 持久实体与关系的问题
- javascript - 如何使用codeigniter上传和移动文件onclick按钮jquery?