sql - 根据列过滤掉行中的重复项

标签 sql oracle plsql

假设我有 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/

相关文章:

sql-server - SQL Server 中的 Max 和 Decode 转换

java - java.sql.Timestamp 时区是否特定?

sql - 我想使用 union all 进行插入,其中有一列从序列中获取值

plsql - 如何合并不同表中的两列并获得单列

java.sql.SQLException : Exhausted Resultset

mysql查询返回匹配元素列表的列表

mysql - SQL : How to get the latest value of an unordered column

sql - 使用 SQL 计算库存在某个地点的天数

c# - PLSQL Oracle Command不允许在同一个命令中出现多个异常

mysql - 查询查找被另一个国家隔开的所有国家对