我有一个这样的postgres表:
C1 C2
-------------------------------------
apple No Thorns
apple No Thorns
orange Thorns
apple No Thorns
pineapple No Thorns
pineapple Thorns
guava No Thorns
guava Thorns
现在我想要那些never
在单个查询中(在 C2 中(的水果的名称(C1(。 如果这很微不足道,我提前道歉。
上面的答案是apple
我会使用group by
和having
来解决这个问题:
select c1
from t
group by c1
having sum( (c2 = 'Thorns')::int) = 0;
我会尝试
Select distinct c1 from table1
Where not exists (
Select 1 from table1 t2 where table1.c1 = t2.c1 and t2.c2 = 'Thorns')