Sql 如何查询';只有';条款

Sql 如何查询';只有';条款,sql,Sql,我正在尝试查询以下数据集: Customer ID Item Purchased 1234 Bread 1235 Peanut Butter 1234 Jelly 1234 Peanut Butter 1234 Jelly 5555 Peanut Butter 5555 Peanut Butter 1235 Jel

我正在尝试查询以下数据集:

Customer ID     Item Purchased
1234            Bread
1235            Peanut Butter
1234            Jelly
1234            Peanut Butter
1234            Jelly
5555            Peanut Butter
5555            Peanut Butter
1235            Jelly
我正试图获得以下预期结果:

Customer ID     Item Purchased
5555            Peanut Butter

在这种情况下,我希望客户5555只购买花生酱,其他什么都没有。

您可以使用
不存在

select t.*
from table t
where not exists (select 1 
                  from table t1 
                  where t1.Customer = t.Customer and 
                        t1.Item <> 'Peanut Butter'
                 );

谢谢你,约格什!你知道如何更新表格吗?请指定你正在使用的数据库引擎标记。请添加更多说明,以便OP能够理解batter。
select Customer 
from table t
where Item = 'Peanut Butter'
group by Customer 
having min(Item) = max(Item);
SELECT DISTINCT(`customer_id`) FROM `tablename` WHERE
`customer_id` IN(SELECT COUNT(DISTINCT(`items_purchased`)) AS `c`, 
`customer_id` FROM `table_name` GROUP BY `customer_id`) 
AND `items_purchased` = 'PEANUT BUTTER'