Sql数据检索问题

时间:2012-10-26 19:38:39

标签: sql oracle plsql

我有以下表格:

顺序

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_no 1已经作为状态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') 

此致 Chaitu

3 个答案:

答案 0 :(得分:1)

这将通过您给定的架构/数据实现您想要的......

SELECT order_number, order_name
FROM order
WHERE order_id NOT IN (SELECT order_event_no FROM order_event WHERE status = 'D')

答案 1 :(得分: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')

答案 2 :(得分: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'
  );