0

我有下表:

命令

Order_id     orde_number    Order_name  

1             12345          iphone

2             67891          samsung 

order_event

order_event_no      status

1                    D

1                    C

2                    C

我写了下面的查询来检索status not in ('D')如下,但它给了我 2 条记录,但查询不应该返回,因为order_no1 已经作为状态D,即使它有第二条记录 C 它不应该包括在内。

select o.order_number,o.order_name 
  from order o 
  join order_event oe 
    on (o.order_id=oe.order_event_no) where oe.status not in ('D') 

问候,柴图

4

3 回答 3

1

这将使用给定的架构/数据完成您想要的...

SELECT order_number, order_name
FROM order
WHERE order_id NOT IN (SELECT order_event_no FROM order_event WHERE status = 'D')
于 2012-10-26T19:43:37.483 回答
0

如果您想排除任何状态为“D”的订单,您需要一个子查询。

select o.order_number,o.order_name 
from order o 
where oe.order_event_no 
NOT IN
    (SELECT order_event_no FROM order_event_no WHERE status = 'D')
于 2012-10-26T19:44:59.533 回答
0

这是等价的。一些 RDBM 会更快地执行它:

Select
  o.order_number,
  o.order_name
from
  order o
where
  not exists (
    select
      'x'
    from
      order_event oe
    where
      oe.order_event_no = o.order_id And
      oe.status = 'D'
  );
于 2012-10-26T19:52:44.047 回答