Mysql 选择特定的组合

Mysql 选择特定的组合,mysql,select,group-by,Mysql,Select,Group By,我相信这很容易。只是很难找到 value_id variation_id 1 1 2 1 1 2 3 2 现在我有了一个组合(1,2)并想要选择变体(编辑)例如,我想要给出(1,2)和得到1。给出(1,3),得到2(/EDIT)我如何才能做到这一点 我考虑了按值分组,并以某种方式保留了每个值命中的变量 Chris如果要选择配对(1,1)(1,2)(2,1)(2,2): 如果相同的(值\u id,变量\u id)组合从未

我相信这很容易。只是很难找到

value_id   variation_id
1          1
2          1
1          2
3          2
现在我有了一个组合(1,2)并想要选择变体(编辑)例如,我想要给出(1,2)和得到1。给出(1,3),得到2(/EDIT)我如何才能做到这一点

我考虑了按值分组,并以某种方式保留了每个值命中的变量


Chris

如果要选择配对(1,1)(1,2)(2,1)(2,2):

如果相同的(值\u id,变量\u id)组合从未出现两次,则:

select variation_id from t 
  where value_id in(1,2)
  group by variation_id
  having count(variation_id)=2;
否则

select variation_id from 
  (select distinct value_id, variation_id from t 
     where value_id in(1,2)) as tmp
  group by variation_id
  having count(variation_id)=2;

哦…谢谢,但这不是我想要的。例如,我想要给出(1,2)和得到1。给出(1,3),得到2。你的方法给了我变量1和变量2。
select variation_id from 
  (select distinct value_id, variation_id from t 
     where value_id in(1,2)) as tmp
  group by variation_id
  having count(variation_id)=2;