and (to_date(t.time,'yyyy-mm-dd hh24:mi:ss')- to_date(fro.time,'yyyy-mm-dd hh24:mi:ss'))>3
以上的sql是有问题的,我要的答案是t.time-fro.time>3秒的sql怎么写,谢谢了。

解决方案 »

  1.   

    --两个时间直接相减得到的是相差的天数
    (to_date(t.time,'yyyy-mm-dd hh24:mi:ss')- 
     to_date(fro.time,'yyyy-mm-dd hh24:mi:ss'))*24*60*60>3
    --例如:
    select (sysdate-to_date('20101111','yyyymmdd'))*24*60*60 from dual
    (SYSDATE-TO_DATE('20101111','YYYYMMDD'))*24*60*60
    -------------------------------------------------
                                               221335
      

  2.   

    1、oracle中两相日期可以相减,但结果的单位是天,可以将天*24*60*60转换成秒。
    2、oracle中两相日期不能相加,但日期可以加上或减去一个数值,这个数值也是天。
    更详细:日期操作
      

  3.   

    其实在10g以上还可以使用数据类型INTERVAL DAY TO SECOND数据类型 
    如: interval '3' second 来表示3秒
    上述条件可以这样写:
    and (to_date(t.time,'yyyy-mm-dd hh24:mi:ss') - interval '3' second >to_date(fro.time,'yyyy-mm-dd hh24:mi:ss'));
      

  4.   


    (to_date(t.time,'yyyy-mm-dd hh24:mi:ss')- to_date(fro.time,'yyyy-mm-dd hh24:mi:ss'))*24*60*60>3
      

  5.   

    非得要这么写就这样(将秒转换成天):
    (to_date(t.time,'yyyy-mm-dd hh24:mi:ss')- to_date(fro.time,'yyyy-mm-dd hh24:mi:ss'))>3/(24*60*60)
      

  6.   

    楼上的都是一个意思--t.time-fro.time>3秒
    and (to_date(t.time,'yyyy-mm-dd hh24:mi:ss')- to_date(fro.time,'yyyy-mm-dd hh24:mi:ss'))>3/(24*60*60);
      

  7.   

    对interval如果不熟悉,还是直接按照Oracle时间计算来做就可以了,oracle中两个日期相减,结果是以天为单位的,可以将天*24*60*60转换成秒。and (to_date(t.time,'yyyy-mm-dd hh24:mi:ss')- to_date(fro.time,'yyyy-mm-dd hh24:mi:ss'))*24*60*60 > 3;
      

  8.   

    我覺得interval在語義上更容易理解