2017-08-09 77 views
0

我有一個postgres表是這樣的:選擇行某一列(C1),其在列(C2)缺少一個特定的值(V1)時,我們通過柱(C1)組

C1       C2  
------------------------------------- 
apple      No Thorns 
apple      No Thorns 
orange      Thorns 
apple      No Thorns 
pineapple     No Thorns 
pineapple     Thorns 
guava      No Thorns 
guava      Thorns 

現在我想要在單個查詢中得到刺(在C2中)的那些水果的名字(C1)。 如果這很微不足道,我很抱歉。

回答上面是apple

回答

1

我會接近這個使用group byhaving

select c1 
from t 
group by c1 
having sum((c2 = 'Thorns')::int) = 0; 
0

我會嘗試

Select distinct c1 from table1 
Where not exists (
Select 1 from table1 t2 where table1.c1 = t2.c1 and t2.c2 = 'Thorns') 
相關問題