情况是这个样子的。
有一个表。有两个时间格式的字段,例如bgtime , edtime.
现在要查询这个表的内容,额外的添加一个显示的列:例如stat
希望根据当前时间来判断是不是在这两个时间之内。
如果是则stat 为0,否则为1。
不知道这种需求如何来处理,也许有的朋友会说用程序来实现不要用数据库。
可是我希望得到回答是:用数据库能不能实现。如果能实现用什么方法。同时,从效率上来考虑
是交由程序处理快还是数据库处理快。
....谢谢了, 大家也可以讨论一下我要下班了,因为太累了,所以明天早上看结果了。

解决方案 »

  1.   


    SQL> select * from a;        ID       STAT BEGINDATE  ENDDATE
    ---------- ---------- ---------- ----------
             2            20-1月 -06 10-4月 -06
             3            20-2月 -06 01-3月 -06
             1            20-12月-06 01-5月 -06SQL> update a set stat=1 where exists(select * from a a1 where a1.id=a.id and sysdate between begind
    ate and enddate);已更新 1 行。  1* update a set stat=0 where exists(select * from a a1 where a1.id=a.id and (sysdate<begindate or 
    SQL> /已更新2行。SQL> 
    SQL> select * from a;        ID       STAT BEGINDATE  ENDDATE
    ---------- ---------- ---------- ----------
             2          1 20-1月 -06 10-4月 -06
             3          0 20-2月 -06 01-3月 -06
             1          0 20-12月-06 01-5月 -06
    自己在照着改改吧
      

  2.   

    udpate a set stat=0 where exists(select * from a a1 where a1.id=a.id and (sysdate<begindate or sysdate>enddate));update a set stat=1 where exists(select * from a a1 where a1.id=a.id and sysdate between begindate and enddate);刚才有些代码没有贴好
      

  3.   

    这样也可以
      1* update a set stat=case when sysdate between begindate and enddate then 1 else 0 end
    SQL> /已更新3行。SQL> select * from a;        ID       STAT BEGINDATE  ENDDATE
    ---------- ---------- ---------- ----------
             2          1 20-1月 -06 10-4月 -06
             3          0 20-2月 -06 01-3月 -06
             1          0 20-12月-06 01-5月 -06
      

  4.   

    select (case when sysdate between bgtime and edtime then 1 else 0 end )as stat,bgtime, edtime from table_name