sql选择具有重复数据的行

时间:2015-05-01 12:47:57

标签: sql oracle

我需要帮助我的关系表中的这个select语句 (供应商和产品都是复合主键,唯一标识每一行

Supplier    Products
ABC         Toys
ABC         Snacks
ZXC         Snacks
ZXC         Food
QWE         Toys
ABC         Food

我需要找到不供应玩具的供应商 所以我只能得到ZXC

我尝试以下但它给了我ABC,ZXC

select distinct Supplier
from table 
where NOT (Products ='Toys');

(我正在使用oracle)我的查询应该如何?感谢

3 个答案:

答案 0 :(得分:5)

select distinct supplier 
from table
where supplier not in (select supplier from table where products = 'Toys')

答案 1 :(得分:2)

如果条件计数等于0(组内没有匹配项),您可以group by Supplier并使用having子句评估为true:

select Supplier
from table 
group by Supplier
having count(case when Products = 'Toys' then Products end) = 0

答案 2 :(得分:1)

使用NOT EXISTS

执行此操作的另一种方法
select distinct
    supplier 
from 
    table t1
where 
    not exists (select * from table t2 where t1.supplier = t2.supplier and t2.products = 'Toys')