2011-12-12 93 views
0

表:查找的ID,其中的值是唯一的在陣列

ID | Value 
----------------- 
1 | red 
1 | black 

2 | green 
2 | black 

陣列( '綠色', '橙色', '黑色');

,我需要得到的結果ID:2

有人能救我的命嗎?

+0

你想要什麼,其獲取基於array..is的所有值的記錄的查詢? – punit

+1

應遵循的規則/要求是什麼?安傑洛的文章會從字面上給你你需要的答案,但如果有規則要遵循,另一種方法也許是可能的......只是說:D – Nonym

回答

3
SELECT id 
FROM table 
WHERE value IN ('green', 'orange', 'black') 
     AND id NOT IN (
      SELECT id 
      FROM table 
      WHERE value NOT IN ('green', 'orange', 'black')) 
2

嘗試:

SELECT id 
FROM table 
group by id 
having sum(case when value IN ('green', 'orange', 'black') then 1 else 0 end) > 0 
and sum(case when value NOT IN ('green', 'orange', 'black') then 1 else 0 end) = 0