Sql检索结果

时间:2012-10-08 18:30:24

标签: sql oracle

我有下面的订单表,我想检索那些未处于订单状态“已处理”和“cacel”状态的记录,这些状态在今天的日期和时间(美国17:00)被“取消”。请帮我完成我的问题。

order id  ordername order_status  cancel_status  order_time           cancel_time 
==============================================================================================    
1         Iphone    processed     cancelled      10/08/2012 16:00:00  10/08/2012 16:00:00        
2         samsung   notprocessed  null           null                 null        
3         nokia     processed     cancelled      10/08/2012 16:00:00  10/08/2012 17:00:00    
4         motorola  notprocessed  null           null         null    
5         HTC       processed     null           10/08/2012 17:00:00  null

我尝试了以下方式,但没有返回任何记录。请帮助我。

SELECT * 
FROM 
    order 
WHERE 
    to_char(order_time,'YYYYMMDD HH24:MI:SS')>To_char(sysdate,YYYYMMDD) || ' '|| '17:00:00' 
and to_char(cancel_time,'YYYYMMDD HH24:MI:SS')>To_char(sysdate,YYYYMMDD) || ' '|| '17:00:00' 
and order_time is null 
and cancel_time is null

2 个答案:

答案 0 :(得分:0)

Select * from order 
where 
( to_char(order_time,'YYYYMMDD HH24:MI:SS')>To_char(sysdate,YYYYMMDD) || ' '|| '17:00:00' 
or order_time is null ) 
and ( to_char(cancel_time,'YYYYMMDD HH24:MI:SS')>To_char(sysdate,YYYYMMDD) || ' '|| '17:00:00' 
or cancel_time is null )

答案 1 :(得分:0)

select * 
from order o 
where o.order_status != 'processed' 
  and o.cancel_status = 'cancelled' 
  and o.order_time < (trunc(sysdate) + 17/24) 
  and o.cancel_time < (trunc(sysdate) + 17/24)