我有一个将元素分配给组的表。每个元素可以存在于多个组中,并且可以多次分配给同一组
看起来像:
element | group
      1 | 1
      1 | 2 
      1 | 3
      2 | 1
      2 | 3
      3 | 2
我正在寻找一个简单的查询,它可以返回分配给第 1 组但不能返回第 2 组的元素。
根据上面提供的数据,这将是元素 2。
我有一个将元素分配给组的表。每个元素可以存在于多个组中,并且可以多次分配给同一组
看起来像:
element | group
      1 | 1
      1 | 2 
      1 | 3
      2 | 1
      2 | 3
      3 | 2
我正在寻找一个简单的查询,它可以返回分配给第 1 组但不能返回第 2 组的元素。
根据上面提供的数据,这将是元素 2。
select distinct element 
from  your_table
where element not in (select element 
                      from your_table 
                      where `group` = 2)
and `group` = 1
    SELECT *
FROM `table` t1
LEFT OUTER JOIN `table` t2
   ON t1.element = t2.element
   AND t2.`group` = 2
WHERE t1.`group` = 1
   AND t2.`group` IS NULL
    像那样:
select element from table 
where group = 1 and not exists 
(select element from table t 
 where t.element=table.element and t.group = 2)