Mysql 查询以查找特定列值不存在的特定行

Mysql Query to find specific rows for which the particular column value don't exist

我的处境非常复杂。我知道 sql 可以实现我的目标。这是我要实现的目标:

我只有一个 table 名为 'data'

我有 4 个列,名称分别为 orderDate、orderID、orderDescription 和 orderType

我的 table 中存在 4 种类型的 'orderType' 值,它们是订单、退款、退货、调整。如果发生任何这种情况,每个 'OrderType' 的 orderid 将保持不变。

现在,我想要实现的是按如下方式过滤我的数据。

Select 只有 table.

中不存在列 'orderType' 值(退货和调整)的那些 orderID 记录

非常期待您的帮助。

select orderid
from data
group by orderid
having sum(ordertype in ('Returned', 'Adjustment')) = 0
   and sum(ordertype = 'Refund') > 0 
   and sum(ordertype = 'Order') > 0 

select orderid
from data
group by orderid
having sum(ordertype not in ('Refund', 'Order')) = 0
and count(distinct ordertype) = 2