首页 > 解决方案 > WHERE/GROUP By Condition - 一个名称但多个值

问题描述

我有下表:

Name          Product         
Bob           Car              
Bob           Apples
Bob           Pears
Bob           Car
John          Apples
John          Pears

无论谁购买了 Product Car,我都想与其他人保持距离。所以,我创建了一个标志:

Name          Product       Flag        
Bob           Car             1  
Bob           Apples          0
Bob           Pears           0  
Bob           Car             1
John          Apples          0
John          Pears           0 

但是我的标志的问题是,即使我做了一个 where 条件并说,告诉我消费者 WHERE 标志!=1,它也会选择 Bob。这是不正确的,因为 Bob 拥有汽车。

我仍然想按产品分组。

如何将上表分成两组?

谢谢!

标签: mysqlsqlhql

解决方案


使用以下查询:-

select name from table where flag!=1
and name not in (select name from table where flag = 1)
group by name

推荐阅读