select 图片 from table where dateval >= 起始时间 and dateval <= 结束时间 and rownum < 2 order by 起始时间 desc

解决方案 »

  1.   


    应该是这样
    select 图片 from table where 起始时间 in (select Max(起始时间) from table where trunc(sysdate) between 起始时间 and 结束时间 )
    假设表中就是这两行数据:
    图片      起始时间    结束时间
    1.gif     2005-1-1    2005-12-31
    2.gif     2005-5-1    2005-5-7下面这条语句,
    select 图片 from table where dateval >= 起始时间 and dateval <= 结束时间 and rownum < 2 order by 起始时间 desc
    选择出的结果是 1.gif问题在于:rownum是选择出未排序前的顺序代号,当排序后这数值不会变化.
    你可以做这样的测试:
    select t.*,rownum from table t where trunc(sysdate) between 起始时间 and 结束时间 order by 起始时间 desc
    你就会发现结果是:
    图片      起始时间    结束时间      rownum
    2.gif     2005-5-1    2005-5-7        2
    1.gif     2005-1-1    2005-12-31      1
      

  2.   

    前面错了select 图片 from ( select 图片 from table where dateval >= 起始时间 and dateval <= 结束时间 order by 起始时间 desc ) where rownum < 2
      

  3.   

    select 图片 from table where 起始时间 in (select Max(起始时间) from table where trunc(sysdate) between 起始时间 and 结束时间 )这个是可能会产生问题的,倘若:
    图片      起始时间    结束时间
    1.gif     2005-1-1    2005-12-31
    2.gif     2005-5-1    2005-5-7
    3.gif     2005-5-1    2005-5-5你将会得到两条纪录,无法保证只得到一条
      

  4.   

    kulama2004(kulama):
    我认为这个问题就要上升到逻辑问题上来了,而不是语句的问题.
    请问你认为应该用哪个图片?是2.jpg还是3.jpg?
    你能保证选出一个,但是你选择的逻辑是什么,按照你逻辑的结果有可能是2.jpg也有可能是3.jpg.
    这取决与2.jpg,3.jpg这两行在数据库中存储的顺序.按照我们的逻辑应该是不允许"起始时间"有重复的, 应该在上面建立唯一索引.
    请问如果按照
    图片      起始时间    结束时间
    1.gif     2005-1-1    2005-12-31
    2.gif     2005-5-1    2005-5-7
    3.gif     2005-5-1    2005-5-5
    这样的资料是什么意思?
    这应该是有人在维护资料时出错了.就算这样
    那么这条语句如何?
    select 图片 from table where 起始时间 in (select Max(起始时间) from table where trunc(sysdate) between 起始时间 and 结束时间 ) order by 结束时间
    在程序中调用时,我只选用第一个值,这样应该没有问题吧
      

  5.   

    之一:
    select 图片 from your_table where sysdate between 起始时间 and 结束时间之二:
    1.首先对齐你的日期。如:2005-1-1 改为:2005-01-01;
    2.select 图片 from your_table where to_char(sysdate,'yyyy-mm-dd') between to_char(起始时间,'yyyy-mm-dd') and to_char(结束时间,'yyyy-mm-dd')
      

  6.   

    to yqwd911(windy) 
    逻辑的问题就不是那么好讨论的问题,你我根本都不知道楼主真正的业务需求,所以也无从谈起
    我只是想就事论事而言可能产生的问题发表自己的看法图片      起始时间    结束时间
    1.gif     2005-1-1    2005-12-31
    2.gif     2005-5-1    2005-5-7
    3.gif     2005-5-1    2005-5-5这种数据完全是可能产生的,因为楼主预先说明start time是一个unique key的字段
      

  7.   

    select * from 
    (select t.* , row_number() over (order by 起始时间 desc,结束时间) rownum2 from table t where 起始时间 <= sysdate  and 结束时间 >=sysdate )
    where rownum2 <2这样的话,即使开始时间相同也可以按结束时间的先后顺序来判断用哪个!!
      

  8.   

    我感觉楼主的意思是找出最接近当前时间的时间段内的图片,如:有下面的记录:
    PIC                            STARTDATE   ENDDATE
    ------------------------------ ----------- -----------
    1.gif                          2005-1-1    2005-12-31
    2.gif                          2005-1-1    2005-7-8
    5.gif                          2005-5-1    2005-5-17在5-1到5-17应该是5.gif,6-20应该选择2.gif
    SQL> select sysdate from dual;SYSDATE
    -----------
    2005-5-12 9SQL> select pic from tt where (sysdate-startdate,enddate-sysdate)=(select min(sysdate-startdate),min(enddate-sysdate) from tt where sysdate>=startdate and sysdate<=enddate);PIC
    ------------------------------
    5.gifSQL> select sysdate+20 from dual;SYSDATE+20
    -----------
    2005-6-1 9:SQL> select pic from tt where (sysdate+20-startdate,enddate-sysdate-20)=(select min(sysdate+20-startdate),min(enddate-sysdate-20) from tt where (sysdate+20)>=startdate and (sysdate+20)<=enddate);PIC
    ------------------------------
    2.gif
    这个相对来说通用一点,楼上的感觉考虑的情况少了点
      

  9.   

    我的意思是在找出最接近当前时间的时间段内的图片,过了这个时间段后再运行以前一个时间段的内容PIC                            STARTDATE   ENDDATE
    ------------------------------ ----------- -----------
    1.gif                          2005-1-1    2005-12-31
    2.gif                          2005-5-1    2005-5-8
    5.gif                          2005-10-1    2005-10-8
    当系统时间运行到5月1日的时候。显示的是2.gif,5月8日之后回到1.gif,当再10月1日-10月8日的时候
    就显示5.gif的内容。然后又回到1.gif的内容。
      

  10.   

    To:space6212327() select min(sysdate-startdate),min(enddate-sysdate) from tt where sysdate>=startdate and sysdate<=enddate
    是有可能不正确的,考虑如下数据:
    2.gif                          2005-5-1    2005-5-8
    5.gif                          2005-5-6    2005-6-8
    就可能没法返回数据了!!
    如果是搂主的要求,
    select * from 
    (select t.* , row_number() over (order by 起始时间 desc,结束时间) rownum2 from table t where 起始时间 <= sysdate  and 结束时间 >=sysdate )
    where rownum2 <2
    完全可以满足了