0

グループに要素を割り当てるテーブルがあります。各要素は多くのグループに存在でき、同じグループに数回割り当てることができます

次のようになります。

element | group
      1 | 1
      1 | 2 
      1 | 3
      2 | 1
      2 | 3
      3 | 2

グループ1に割り当てられているが、グループ2には割り当てられていない要素を返すことができる単純なクエリを探しています。

上記の提示されたデータによると、これは要素2になります。

4

3 に答える 3

2
select distinct element 
from  your_table
where element not in (select element 
                      from your_table 
                      where `group` = 2)
and `group` = 1
于 2012-06-23T20:28:34.853 に答える
0

そのように:

select element from table 
where group = 1 and not exists 
(select element from table t 
 where t.element=table.element and t.group = 2)
于 2012-06-23T20:31:15.237 に答える
0
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
于 2012-06-23T20:28:12.130 に答える