首页 > 解决方案 > 从一列中选择一个不同的变量

问题描述

这是我的桌子的样本:

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,因为它有现金和支票支付类型。

标签: sqloracle

解决方案


您可以使用 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在这里


推荐阅读