假设我有 2 列:
Fruit Condition
apple unripe
banana ripe
apple ripe
banana moldy
peach moldy
peach ripe
apple ripe
pineapple soldout
我只想知道哪些水果是成熟的还是未成熟的,没有发霉或卖完了(只有苹果)
Select fruit
from example
where (condition <> 'moldy' or condition <> 'soldout')
and (condition = 'ripe' or condition = 'unripe')
group by fruit
不工作
最佳答案
您正在使用 or
在一个不是。这是错误的做法。
使用:
where not (condition = 'moldy' or condition = 'soldout')
或使用
where (condition <> 'moldy' and condition <> 'soldout')
那么,我假设您只想要成熟或未成熟的水果。
select distinct Fruit
from Example E1
where Condition in ('ripe','unripe')
and not exists
(
select E2.Fruit
from Example E2
where E1.Fruit = E2.Fruit
and E2.Condition in ('moldy','soldout')
)
关于sql - 根据列过滤掉行中的重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39874368/